对仅更改其他属性的图层属性进行动画处理? [英] Animate a layer property which simply changes other properties?

查看:78
本文介绍了对仅更改其他属性的图层属性进行动画处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个 CAGradientLayer

.startPoint设置动画非常容易 .endPoint

现在想象一个浮动 spinLike 只需同时设置它们两者

Now imagine a float spinLike which simply sets both of them at once.

{因此,您不必制作两个不同的动画,而只需制作动画 spinLike 。}

{So, instead of having two different animations, you could simply animate spinLike.}

所以类似..

class CustomGradientLayer: CAGradientLayer {

    @objc var spinLike: CGFloat = 0 {

        didSet {

            startPoint = CGPoint( ... )
            endPoint = CGPoint( ... )
            setNeedsDisplay()
        }
    }
}

spinLike 动画$ b

class Test: UIView {

     ...
     g = CustomGradientLayer()
     a = CABasicAnimation(keyPath: "spinLike")
     ...
     g.add(a, forKey: nil)
     ...

但是。

它不起作用, startPoint endPoint 根本不会移动。

It doesn't work, startPoint and endPoint are not moved at all.

怎么了?

注意-可悲的是,您似乎无法 @ NSManaged 一个具有didSet ...的属性...

Note - tragically it seems you can not @NSManaged a property which has a didSet...

注意-只需覆盖绘制循环,即可轻松制作自己的自定义动画 >。

周围有很多例子。这是您的操作方式:

There are many examples of this around. This is how you do it:

class CircleProgressLayer: CALayer {

    @NSManaged var progress: CGFloat

    override class func needsDisplayForKey(key: String) -> Bool {

        if key == "progress" {
            return true
        }
        return super.needsDisplayForKey(key)
    }

    override func draw(in ctx: CGContext) {

        path.fill() etc etc... your usual drawing code
    }
}

不幸的是,我的问题是

通过动画属性 spinLike

想要更改每个框架现有的普通动画属性(在示例中, .startPoint .endPoint

I simply want to change each frame existing ordinary animatable properties (in the example, .startPoint and .endPoint )

您如何做?

注意!您不能在 drawInContext .startPoint .endPoint >-您将试图修改只读层

Note! You can't change .startPoint and .endPoint in drawInContext - you'd be attempting to modify read-only layer

推荐答案

要制作自定义属性的动画,应使用 @NSManaged 对其进行标记。分配新值时,不应强制重绘。相反,您应该覆盖 needsDisplay(forKey :)

To animate custom properties, you should mark them with @NSManaged. You should not force redrawing when you assign a new value. Instead, you should overwrite needsDisplay(forKey:).

class CustomedGradLayer: CAGradientLayer {
    @NSManaged var spinLike: CGFloat

    class func needsDisplay(forKey key: String) -> Bool {
        return key == "spinLike" || super.needsDisplay(forKey: key)
    }

    class func defaultValue(forKey key: String) -> Any? {
        return key == "spinLike" ? CGFloat(0) : super.defaultValue(forKey: key)
    }
}

最后,您应该根据 Apple文档

我在Swift中写了一个小项目前。它演示了具有Koch曲线深度的自定义图层动画。

I wrote a small project in Swift some months ago. It demonstrates custom layer animations with the depth of a Koch curve.

这是图层类的代码:

class KochLayer: CALayer {
    fileprivate let kPI = CGFloat(Double.pi)
    @NSManaged var depth : CGFloat
    var midPoint: CGPoint {
        get {
            let theBounds = self.bounds

            return CGPoint(x: theBounds.midX, y: theBounds.midY)
        }
    }
    var color: CGColor!

    override class func defaultValue(forKey inKey: String) -> Any? {
        return inKey == kDepthKey ? 0.0 : super.defaultValue(forKey: inKey)
    }

    override class func needsDisplay(forKey inKey: String) -> Bool {
        if inKey == kDepthKey {
            return true
        }
        else {
            return super.needsDisplay(forKey: inKey)
        }
    }

    override init() {
        super.init()
    }

    override init(layer inLayer: Any) {
        super.init(layer: inLayer)
        if let theLayer = inLayer as? KochLayer {
            depth = theLayer.depth
            color = theLayer.color
        }
    }

    required init(coder inCoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func pointWithRadius(_ inRadius: CGFloat, angle inAngle: CGFloat) -> CGPoint {
        let theCenter = midPoint

        return CGPoint(x: theCenter.x + inRadius * sin(inAngle),
            y: theCenter.y - inRadius * cos(inAngle));
    }

    override func draw(in inContext: CGContext) {
        let theBounds = self.bounds
        let theRadius = fmin(theBounds.width, theBounds.height) / 2.0
        let thePoints: [CGPoint] = [
            pointWithRadius(theRadius, angle:0.0),
            pointWithRadius(theRadius, angle:2 * kPI / 3.0),
            pointWithRadius(theRadius, angle:4 * kPI / 3.0)
        ]
        let thePath = CGMutablePath()

        inContext.setLineWidth(0.5)
        inContext.setLineCap(.round)
        inContext.setLineJoin(.round)
        inContext.setFillColor(color)
        thePath.move(to: thePoints[0])
        for i in 0..<3 {
            addPointsToPath(thePath, fromPoint:thePoints[i], toPoint:thePoints[(i + 1) % 3], withDepth:self.depth)
        }
        inContext.addPath(thePath)
        inContext.fillPath()
    }

    func addPointsToPath(_ inoutPath: CGMutablePath, fromPoint inFromPoint: CGPoint, toPoint inToPoint: CGPoint, withDepth inDepth: CGFloat) {
        var thePoints = Array<CGPoint>(repeating: inFromPoint, count: 5)

        thePoints[4] = inToPoint;
        if inDepth <= 1.0 {
            curveWithWeight(inDepth, points:&thePoints)
            for i in 1..<5 {
                inoutPath.addLine(to: thePoints[i])
            }
        }
        else {
            let theDepth = inDepth - 1;

            curveWithWeight(1.0, points:&thePoints)
            for i in 0..<4  {
                addPointsToPath(inoutPath, fromPoint:thePoints[i], toPoint:thePoints[i + 1], withDepth:theDepth)
            }
        }
    }

    func curveWithWeight(_ inWeight: CGFloat, points inoutPoints: inout [CGPoint]) {
        let theFromPoint = inoutPoints[0]
        let theToPoint = inoutPoints[4]
        let theFactor = inWeight / (2 * sqrt(3))
        let theDelta = CGSize(width: theToPoint.x - theFromPoint.x, height: theToPoint.y - theFromPoint.y);

        inoutPoints[1] = CGPoint(x: theFromPoint.x + theDelta.width / 3,
            y: theFromPoint.y + theDelta.height / 3)
        inoutPoints[2] = CGPoint(x: theFromPoint.x + theDelta.width / 2 + theFactor * theDelta.height,
            y: theFromPoint.y + theDelta.height / 2 - theFactor * theDelta.width);
        inoutPoints[3] = CGPoint(x: theToPoint.x - theDelta.width / 3,
            y: theToPoint.y - theDelta.height / 3)
    }
}

这篇关于对仅更改其他属性的图层属性进行动画处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆