gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:18:44 26 4
gpt4 key购买 nike

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

文档说该属性是可动画,但默认情况下不是动画,我无法查明问题所在。有什么建议在哪里看?

这是我的代码:

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
}
}

CAShapeLayer strokeEnd animation

最佳答案

您描述的将图层的 strokeEnd 操作设置为 NSNull 的方法可行,但它有点像大锤。当您这样做时,您将永远终止图层的 strokeEnd 属性的隐式动画。

如果这就是您想要的,那没关系。但是,我倾向于使用 David Rönnqvist 在您链接的答案中列出的第二种方法:Making your layer change inside a CATransaction begin/commit block。这是 David 的回答中的代码(非常好,他的帖子一如既往)。

[CATransaction begin];
[CATransaction setDisableActions:YES];
// change your property here
yourShapeLayer.strokeEnd = 0.7;
[CATransaction commit]; // animations are disabled until here...

该代码在 Objective-C 中。将它翻译成 Swift 也不错:

CATransaction.begin()
CATransaction.setDisableActions(true)
yourShapeLayer.strokeEnd = 0.7
CATransaction.commit()

关于ios - 我可以在没有动画的情况下更改 strokeEnd 属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30150298/

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