CAAnimation 路径仅在 view/layer 范围内

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 属性,但没有用

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