首页 > swift > CAAnimation路径仅在视图/图层的边界内

CAAnimation路径仅在视图/图层的边界内 (CAAnimation path only within bounds of view/layer)

问题

我有一个UIView,它是屏幕的一半。点击该视图,我创建一个CAShapeLayer,然后设置其路径动画,如下所示:

let animation = CABasicAnimation(keyPath: "path")
    animation.toValue = endShape
    animation.duration = 0.7
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) // animation curve is Ease Out
    animation.fillMode = kCAFillModeBoth
    animation.isRemovedOnCompletion = true

但问题是形状层扩展超出视图的边界。是否有可能将其保持在界限范围内?

还尝试了不同的masksToBounds属性,但它没有用

解决方法

我找到了答案基本上我有一个错误的视图/层次结构。应该在超级图层上设置了masksToBounds属性,但我在动画层本身上设置了它

问题

I have a UIView that is half of the screen. On tap on that view, I create a CAShapeLayer and then animate its path, like so:

let animation = CABasicAnimation(keyPath: "path")
    animation.toValue = endShape
    animation.duration = 0.7
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) // animation curve is Ease Out
    animation.fillMode = kCAFillModeBoth
    animation.isRemovedOnCompletion = true

But the problem is that the shape layer expands beyond the bounds of the view. Is it possible to keep it within the bounds?

Also tried varying masksToBounds property, but it didn't work

解决方法

I found an answer Basically I had a wrong view/layer hierarchy. masksToBounds property should have been set on the superlayer, but I had it on the animated layer itself

相似信息