如何“可选化”基于块的 UIView 动画?
我的自定义控件有一个方法 -setValue:animated:,它采用 animated
标志。
在 iOS 4 之前,我会这样编写动画:
if (animated) {
[UIView beginAnimations:@"Foo"];
[UIView setAnimationDuration:5.0];
}
// ... layout views ...
if (animated) {
[UIView commitAnimations];
}
现在我这样写:
[UIView animateWithDuration:(animated ? 5.0 : 0.0) animations:^{
// ... layout views ...
}];
但是: 这会导致某些元素没有动画!
我多次调用此方法(第一次没有动画,第二次有动画),因此第二次动画被取消,将我的新帧设置为“硬”(没有动画)。
如何用块方法实现可选动画?
My custom control has a method -setValue:animated:, which takes an animated
flag.
Before iOS 4, I would have written the animation thus:
if (animated) {
[UIView beginAnimations:@"Foo"];
[UIView setAnimationDuration:5.0];
}
// ... layout views ...
if (animated) {
[UIView commitAnimations];
}
Now I wrote this:
[UIView animateWithDuration:(animated ? 5.0 : 0.0) animations:^{
// ... layout views ...
}];
BUT: this leads to some elements not animating!
I call this method more than just once (first time without, second time with animation), so the second time round the animation is cancelled, setting my new frame "hard" (without animation).
How to implement optional animation with the blocks approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在块中定义要进行的所有更改。然后,如果您想要动画更改,则可以将块提供给 UIView animate...,或者直接执行它以进行不带动画的更改。
You can define all of the changes you want to make in a block. You then either feed the block to
UIView animate...
if you want the changes animated, or execute it directly to make the changes without animation.