将 UIView 的 CALayer 动画化为圆角
我正在尝试为我的视图的拐角设置动画。 cornerRadius 属性被列为可动画,但我似乎无法让它工作。实际上,我也无法获得任何其他属性来设置动画,但我感兴趣的是角。这是我的代码,非常简单:
[CATransaction begin];
[CATransaction setValue: [NSNumber numberWithFloat: 2.0f] forKey:kCATransactionAnimationDuration];
self.myView.layer.cornerRadius = 50.0f;
[CATransaction commit];
伙计们,我在这里缺少什么?边角变成圆角,但它是瞬时的,而不是需要 2 秒。
I'm trying to animate the rounding of the corners of my view. The cornerRadius property is listed as animatible, but I can't seem to get it to work. I actually can't get any of the other properties to animate either, but the corners are what I'm interested in. Here's my code, and it's pretty darn simple:
[CATransaction begin];
[CATransaction setValue: [NSNumber numberWithFloat: 2.0f] forKey:kCATransactionAnimationDuration];
self.myView.layer.cornerRadius = 50.0f;
[CATransaction commit];
What am I missing here guys? The corners become rounded, but it's instantaneous instead of taking 2 seconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Swift 4.2/5:
Swift 3/4.0:
更新:
正如 Mark 所注意到的,使用
#keyPath
更简洁描述要动画的属性:Swift 4.2/5:
Swift 3/4.0:
Update:
As Mark noticed, it's cleaner to use
#keyPath
to describe the property to be animated:看来你想多了。我在不使用 CoreAnimation 的情况下也能过得去。这就是我所做的:
Seems like you're overthinking it. I was able to get by without using CoreAnimation. Here's what I did: