animation只改变其他属性的图层属性?

想象一下CAGradientLayer

.startPoint.endPoint进行animation制作非常简单。

现在想象一个浮动的spinLike ,它只是简单地将它们同时设置

{所以,不要有两个不同的animation,你可以简单地为spinLikeanimation。

所以像..

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

animationspinLike

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

但。

它不起作用, startPointendPoint根本不移动。

哪里不对?


注 – 悲剧似乎你不能@NSManaged一个属性有一个didSet …

在这里输入图像说明


注意 – 只需重写绘制循环就可以轻松制作自己的自定义animation

有很多这样的例子。 这是你如何做到的:

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

不幸的是我的问题在这里

与实际绘制无关:

通过animation的财产spinLike

我只是想改变每个框架现有的普通animation属性(在例子中, .startPoint.endPoint

你怎么做到这一点?

注意! 您不能在drawInContext更改.startPoint.endPoint – 您将attempting to modify read-only layer

要为自定义属性设置animation,您应该使用@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文档实现图层的绘制。

几个月前我在Swift写了一个小项目。 它演示了一个Koch曲线深度的自定义图层animation。

这是图层类的代码:

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