为什么我的 CAShapeLayer 没有动画? (iOS Swift)
Why is my CAShapeLayer not animating? (iOS Swift)
在我的一个 ViewController 中,我有 addStatus()
,粘贴在下面,在我的 viewDidLoad
中。我希望这个圆圈的 lineWidth
有动画效果,但它似乎并没有这样做。在寻找原因时,我在 Animating Layer Content 上找到了 Apple 文档的这一部分。我认为重要的部分在这里注明:
If you want to use Core Animation classes to initiate animations, you must issue all of your Core Animation calls from inside a view-based animation block. The UIView class disables layer animations by default but reenables them inside animation blocks. So any changes you make outside of an animation block are not animated. Listing 3-5 shows an example of how to change a layer’s opacity implicitly and its position explicitly. In this example, the myNewPosition variable is calculated beforehand and captured by the block. Both animations start at the same time but the opacity animation runs with the default timing while the position animation runs with the timing specified in its animation object.
当我查找为什么这可能不是动画时,我读了这篇文章并假设它意味着我应该将我的 CAAnimation 放在 UIView 动画块中。当放置在 rootViewController
中时,此功能在没有动画块的空白应用程序中运行良好,但在我的辅助 viewController
中似乎没有动画。任何提示都会非常有帮助。谢谢!
func addStatus() {
UIView.animateWithDuration( NSTimeInterval.infinity, animations: { () -> Void in
let patientZeroIndicator = CAShapeLayer()
let radius:CGFloat = 20.0
let center:CGPoint = CGPointMake(self.view.frame.width - radius - 10, radius + 10)
let startAngle = 0.0
let endAngle = 2.0 * Double(M_PI)
patientZeroIndicator.lineWidth = 10.0
patientZeroIndicator.fillColor = UIColor(netHex: cs_red).CGColor
patientZeroIndicator.strokeColor = UIColor.whiteColor().CGColor
patientZeroIndicator.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true).CGPath
self.view.layer.addSublayer(patientZeroIndicator)
// Create a blank animation using the keyPath "cornerRadius", the property we want to animate
let pZeroAnimation = CABasicAnimation(keyPath: "lineWidth")
// Define the parameters for the tween
pZeroAnimation.fromValue = 10.0
pZeroAnimation.toValue = 5.0
pZeroAnimation.autoreverses = true
pZeroAnimation.duration = 3.0
pZeroAnimation.repeatDuration = CFTimeInterval.infinity
pZeroAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.25, 0, 0.75, 1)
// Finally, add the animation to the layer
patientZeroIndicator.addAnimation(pZeroAnimation, forKey: "lineWidth")
})
}
in my viewDidLoad
.
那是你的问题。 viewDidLoad
方法在视图添加到 window 之前运行。您不能将动画添加到视图,除非它在 window 中。改为在 viewDidAppear:
中调用 addStatus
。
此外,不要在动画块中创建新层。在 viewDidLoad
.
中创建图层
在我的一个 ViewController 中,我有 addStatus()
,粘贴在下面,在我的 viewDidLoad
中。我希望这个圆圈的 lineWidth
有动画效果,但它似乎并没有这样做。在寻找原因时,我在 Animating Layer Content 上找到了 Apple 文档的这一部分。我认为重要的部分在这里注明:
If you want to use Core Animation classes to initiate animations, you must issue all of your Core Animation calls from inside a view-based animation block. The UIView class disables layer animations by default but reenables them inside animation blocks. So any changes you make outside of an animation block are not animated. Listing 3-5 shows an example of how to change a layer’s opacity implicitly and its position explicitly. In this example, the myNewPosition variable is calculated beforehand and captured by the block. Both animations start at the same time but the opacity animation runs with the default timing while the position animation runs with the timing specified in its animation object.
当我查找为什么这可能不是动画时,我读了这篇文章并假设它意味着我应该将我的 CAAnimation 放在 UIView 动画块中。当放置在 rootViewController
中时,此功能在没有动画块的空白应用程序中运行良好,但在我的辅助 viewController
中似乎没有动画。任何提示都会非常有帮助。谢谢!
func addStatus() {
UIView.animateWithDuration( NSTimeInterval.infinity, animations: { () -> Void in
let patientZeroIndicator = CAShapeLayer()
let radius:CGFloat = 20.0
let center:CGPoint = CGPointMake(self.view.frame.width - radius - 10, radius + 10)
let startAngle = 0.0
let endAngle = 2.0 * Double(M_PI)
patientZeroIndicator.lineWidth = 10.0
patientZeroIndicator.fillColor = UIColor(netHex: cs_red).CGColor
patientZeroIndicator.strokeColor = UIColor.whiteColor().CGColor
patientZeroIndicator.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true).CGPath
self.view.layer.addSublayer(patientZeroIndicator)
// Create a blank animation using the keyPath "cornerRadius", the property we want to animate
let pZeroAnimation = CABasicAnimation(keyPath: "lineWidth")
// Define the parameters for the tween
pZeroAnimation.fromValue = 10.0
pZeroAnimation.toValue = 5.0
pZeroAnimation.autoreverses = true
pZeroAnimation.duration = 3.0
pZeroAnimation.repeatDuration = CFTimeInterval.infinity
pZeroAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.25, 0, 0.75, 1)
// Finally, add the animation to the layer
patientZeroIndicator.addAnimation(pZeroAnimation, forKey: "lineWidth")
})
}
in my
viewDidLoad
.
那是你的问题。 viewDidLoad
方法在视图添加到 window 之前运行。您不能将动画添加到视图,除非它在 window 中。改为在 viewDidAppear:
中调用 addStatus
。
此外,不要在动画块中创建新层。在 viewDidLoad
.