TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.