Saturday, 31 August 2019

Modify a value during a CAAnimation BOUNTY

Say you have a simple animation

    let e : CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    e.duration = 2.0
    e.fromValue = 0 
    e.toValue = 1.0
    e.repeatCount = .greatestFiniteMagnitude
    self.someLayer.add(e, forKey: nil)

of a layer

private lazy var someLayer: CAShapeLayer

It's quite easy to read the value of strokeEnd each animation step.

Just change to a custom layer

private lazy var someLayer: CrazyLayer

and then

class CrazyLayer: CAShapeLayer {
    override func draw(in ctx: CGContext) {
        print("Yo \(presentation()!.strokeEnd)")
        super.draw(in: ctx)
    }
}

It would be very convenient to actually be able to set the property values of the layer at each step.

Example,

class CrazyLayer: CAShapeLayer {
    override func draw(in ctx: CGContext) {
        print("Yo \(presentation()!.strokeEnd)")
        strokeStart = presentation()!.strokeEnd - 0.3
        super.draw(in: ctx)
    }
}

Of course you can't do that -

attempting to modify read-only layer

Perhaps you could have a custom animatable property

class LoopyLayer: CAShapeLayer {
    @NSManaged var trick: CGFloat

    override class func needsDisplay(forKey key: String) -> Bool {
        if key == "trick" { return true }
        return super.needsDisplay(forKey: key)
    }

    ... somehow for each frame
    strokeEnd = trick * something
    backgroundColor = alpha: trick
}

How can I "manually" set the values of the animatable properties, each frame?

Is there perhaps a way to simply supply (override?) a function which calculates the value each frame?? How to set values each frame, of some of the properties, from a calculation of other properties or perhaps another custom property?



from Modify a value during a CAAnimation BOUNTY

No comments:

Post a Comment