BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

How to work with VectorArithmetic and Accelerate

Forums > SwiftUI

Hey guys, I am trying to animate curves in SwiftUI, I have a BezierShape defined like this:

struct BezierShape: Shape {

    var startPoint: CGPoint
    var curves: [Curve]

    init(startPoint: CGPoint, curves: [Curve]) {
        self.startPoint = startPoint
        self.curves = curves
    }

    func path(in rect: CGRect) -> Path {
        var path = Path()
        let width = rect.size.width
        let height = rect.size.height

        // Move the the start point
        let startPoint = CGPoint(x: startPoint.x*width, y: startPoint.y*height)
        path.move(to: startPoint)

        // Draw the curves
        for c in curves {
            path.addCurve(to: CGPoint(x: c.destination.x*width, y: c.destination.y*height),
                          control1: CGPoint(x: c.control1.x*width, y: c.control1.y*height),
                          control2: CGPoint(x: c.control2.x*width, y: c.control2.y*height))
        }

        return path
    }
}

struct Curve {
    var destination: CGPoint
    var control1: CGPoint
    var control2: CGPoint
}

To animate the bezier shape, I am changing the values passed as parameters to this struct, but since I didn't include an animatableData property, SwiftUI doesn't know how to animate these values. I know that to create this property I must use values that conform to VectorArithmetic, so for the start point property I can use its x- and y-values, as these conform to VectorArithmetic. But the array of Curve values doesn't. And I don't know how I can make it conform to VectorArithmetic. If someone could help me with this that would be greatly appreciated!

P.S. I've found this article from Swift with Majid explaining how to make an array conform to VectorArithmetic with the use of Accelerate, this gave me a better understanding of the situation and how I could approach it, but I don't know Accelerate nor VectorArithmetic enough to be able to find a solution with this article.

2      

Take a look at https://github.com/hyperjeff/Accelerate-in-Swift. Several examples of using Accelerate, some of which may help you.

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.