【问题标题】:Animate a layer property which simply changes other properties?为仅更改其他属性的图层属性设置动画?
【发布时间】:2017-11-07 18:12:23
【问题描述】:

想象一下CAGradientLayer

.startPoint.endPoint 制作动画非常容易。

现在想象一个浮点数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 的属性...


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

这方面的例子很多。这就是你的做法:

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 - 您将成为 attempting to modify read-only layer

【问题讨论】:

  • 你应该覆盖draw(in context: CGContext)而不是drawInContext(ctx: CGContext)
  • (嗯,这不是@clemens - 我确实测试了draw#in - 我只是出于习惯打了旧的...... :))
  • 您的问题有进展吗?如果startPointendPoint 始终与spinLike 相关,则可以删除这些属性并在draw(in:) 中计算它们的值。

标签: ios animation core-animation cabasicanimation cagradientlayer


【解决方案1】:

要为自定义属性设置动画,您应该用@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 写了一个小项目。它演示了具有 Koch 曲线深度的自定义图层动画。

这是图层类的代码:

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

【讨论】:

  • CGFloat 基于double 而不是Objective C 中的类。因此,您不能为此使用可选的。您应该根据我的答案中的示例声明它,不带问号。
  • 这里有一个小(不是很重要)注释...也许dynamic 会比@NSManaged 更好,因为Apple 明确地将后者与CoreData('The Swift Programming Language /斯威夫特 4 / 第 874 页)
  • @Alladinian 对应的 Objective-C 代码将是 @dynamic,这与 Swift 中的动态关键字不同,所以很遗憾 dynamic 在这里不起作用。
  • @DavidRönnqvist 由于我熟悉您在这个主题上的知识,我想我的立场是正确的!有什么区别?
  • @Alladinian Swift 的 dynamic 意味着将使用 Objective-C 消息发送动态分派访问(即使在 SE-0160 解耦 @obcdynamic 之后,这在实践中仍然是正确的,因为有没有替代运行时)。 Objective-C 的@dynamic 意味着属性的访问器将动态提供(类将在运行时计算出来)。 Core Animation 为动画属性创建了自己的访问器,但我很难找到解释原因的参考。我好像记得是打电话给needsDisplay(forKey:)action(forKey:)等等。
猜你喜欢
  • 1970-01-01
  • 2010-09-30
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2013-06-20
相关资源
最近更新 更多