gpt4 book ai didi

ios - 为什么我的 CAShapeLayer 没有动画? (iOS swift )

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:58 25 4
gpt4 key购买 nike

在我的一个 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 动画 block 中。当放置在 rootViewController 中时,此函数在没有动画 block 的空白应用程序中运行良好,但在我的辅助 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 方法在 View 被添加到窗口之前运行。除非它在窗口中,否则不能向 View 添加动画。在 viewDidAppear: 中调用 addStatus

此外,不要在动画 block 中创建新层。在 viewDidLoad 中创建图层。

关于ios - 为什么我的 CAShapeLayer 没有动画? (iOS swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com