我可以在没有动画的情况下更改strokeEnd属性吗?

Tie*_*eme 15 cashapelayer ios swift

我似乎无法从strokeEnd我的CAShapeLayer 的属性中删除动画.

该文档说该属性animatable但默认情况下没有动画,我无法查明问题.有什么建议去哪儿看?

这是我的代码:

class ViewController: UIViewController {

    let circle = CAShapeLayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Circle
        circle.fillColor = UIColor.clearColor().CGColor
        circle.strokeColor = UIColor.blackColor().CGColor
        circle.lineWidth = 10
        circle.strokeEnd = 0
        circle.lineJoin = kCALineJoinRound
        circle.path = UIBezierPath(ovalInRect: CGRectMake(60, 140, 200, 200)).CGPath
        circle.actions = ["strokeEnd" : NSNull()]

        // Show Button
        let showButton = UIButton(frame: CGRectMake(40, 40, 240, 40))
        showButton.addTarget(self, action: "showButton", forControlEvents: UIControlEvents.TouchUpInside)
        showButton.setTitle("Show circle", forState: UIControlState.Normal)
        showButton.backgroundColor = UIColor.greenColor()

        // Add to view
        self.view.layer.insertSublayer(circle, atIndex: 1)
        self.view.addSubview(showButton)
    }

    func showButton() {
        circle.strokeEnd = 1
    }
}
Run Code Online (Sandbox Code Playgroud)

CAShapeLayer strokeEnd动画

Dun*_*n C 26

您描述的将图层的strokeEnd动作设置为NSNull的方法有效,但它有点像大锤.当你这样做时,你永远杀死你的图层的strokeEnd属性的隐式动画.

如果这就是你想要的,那没关系.但是,我倾向于使用DavidRönnqvist在您链接的答案中列出的第二种方法:在CATransaction begin/commit块中更改图层.以下是大卫答案的代码(这很好,因为他的帖子总是如此).

[CATransaction begin];
[CATransaction setDisableActions:YES];
// change your property here 
yourShapeLayer.strokeEnd = 0.7;
[CATransaction commit]; // animations are disabled until here...
Run Code Online (Sandbox Code Playgroud)

该代码在Objective-C中.将它翻译成Swift并不算太糟糕:

CATransaction.begin()
CATransaction.setDisableActions(true)
yourShapeLayer.strokeEnd = 0.7
CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)


Tie*_*eme 5

我已经找到问题了。这解决了它:

circle.actions = ["strokeEnd" : NSNull()]
Run Code Online (Sandbox Code Playgroud)

更多信息可以在这里找到:Change CAShapeLayer without Animation