Custom animation of two properties in CALayer by a new custom property

I can animate a CALayer object by changing a property using CABasicAnimation as with this code:

let animationOfPropertyOpacity = CABasicAnimation(keyPath: "opacity")
animationOfPropertyOpacity.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animationOfPropertyOpacity.fromValue = 0
animationOfPropertyOpacity.toValue = 1
myLayer.addAnimation(animationOfPropertyOpacity, forKey: "fade")

In this case the animation will create a linear interpolation between 0 to 1. I can do the same with other properties like scale, position or rotation.

What If I want to move from point A (0,3) to point B (3,0) but making a circle respect to point C (0,0)?

I want to keep using fromValue to toValue, but animating a custom property that I've made called rotation. Rotation will calculate each interpolation value based on point C as center and making a radius from that center.

Where do I need to put the function that calculates each step?

I've tried subclassing CALayer and adding a custom property called rotation. Then adding code to needsDisplayForKey to redraw the layer:

@NSManaged var rotation: CGFloat

override class func needsDisplayForKey(key: String) -> Bool {
       if (key == "rotation") {
            return true
        }

        return super.needsDisplayForKey(key)
    }

This calls drawInContext and makes the whole CALayer black. I have a print in drawInContext and it doesn't get called when changing other properties like opacity, it seems it's not necessary to redraw the layer.

There should be a method that is being called each frame of the animation in CALayer where I could implement a function before applying the changes.

I'm also unable to find the right place to implement the functionality of the custom property that I've created to actually reflect the change even without animation. Let's say a property customOpacity that just inverts the effects of opacity and updating the layer accordingly. I only could make that through a function updateRotation(rotation: CGFloat) .

链接地址: http://www.djcxy.com/p/74176.html

上一篇: 在CATransaction中动画自定义CALayer属性

下一篇: 通过新的自定义属性在CALayer中自定义动画的两个属性