ios - 动画层属性,它只是改变其他属性?

标签 ios animation core-animation cabasicanimation cagradientlayer

想象一个 CAGradientLayer

.startPoint.endPoint 的动画非常容易。

现在想象一个 float spinLike,它只是同时设置它们

{因此,您可以简单地为 spinLike 设置动画,而不是使用两个不同的动画。

所以像..

class CustomGradientLayer: CAGradientLayer {
    
    @objc var spinLike: CGFloat = 0 {
        
        didSet {
            
            startPoint = CGPoint( ... )
            endPoint = CGPoint( ... )
            setNeedsDisplay()
        }
    }
}

动画spinLike...

class Test: UIView {

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

但是。

不行,startPointendPoint根本就没动

怎么了?


注意 - 不幸的是你似乎不能 @NSManaged 一个有 didSet 的属性...

enter image description here


请注意 - 只需重写绘制循环即可轻松制作您自己的自定义动画

这方面的例子很多。这是你如何做的:

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)

你是怎么做到的?

注意!您不能在 drawInContext 中更改 .startPoint.endPoint - 您将尝试修改只读层

最佳答案

要使自定义属性具有动画效果,您应该使用 @NSManaged 标记它们。分配新值时不应强制重绘。相反,您应该覆盖 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 documentation实现图层的绘制。 .

几个月前我用 Swift 写了一个小项目。它演示了具有科赫曲线深度的自定义层动画。

这是图层类的代码:

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)
    }
}

关于ios - 动画层属性,它只是改变其他属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47164762/

相关文章:

ios - 将 View Controller 呈现为对话框模式

iOS Metal : The fastest way to read read-only data?

ios - 点击消息不会打开 AppStore 中的应用程序页面

java - 为什么我的动画不可见?

ios - 使用 [UIView animateKeyframesWithDuration] 为 UIView 设置动画的奇怪行为

iphone - 在 iOS 上,为什么更改 CALayer 的帧不会导致动画?

ios - 不将动画重新启动到根 Controller

android - 是否可以创建免费增值订阅 HTML 应用程序?

iphone - 具有多个 View 的 block 转换

image - 使用 CSS3 在屏幕上移动图像