UIAnimation阻止奇怪的行为(UIAnimation block weird behavior)

编程入门 行业动态 更新时间:2024-10-22 16:40:48
UIAnimation阻止奇怪的行为(UIAnimation block weird behavior)

大家好,感谢您的阅读,

我试图使用动画块制作简单的波动画:

[UIView animateWithDuration:0.6 delay:i*delay options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAutoreverse animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y-WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:^(BOOL finished) { tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y+WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); }];

问题是当动画结束时,一个奇怪的跳跃在我尝试将视图返回到它之前的状态时(因为我正在使用重复效果)注意完成块。

如果有人遇到这样的问题请分享,

再次感谢。

Hello all and thanks for reading,

Im trying to make a simple wave animation using animation block:

[UIView animateWithDuration:0.6 delay:i*delay options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAutoreverse animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y-WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:^(BOOL finished) { tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y+WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); }];

The problem is when the animation is over, a weird jump sneaks in while im trying to return the views to it's previous state (because i'm using repeat effect) note the completion block.

If anyone encountered such problem please share,

Thanks again.

最满意答案

这与完成块从视图的模型值触发相对于图层更新的时间有关,该模型值设置为动画的中间。 我发现使这种事情100%的时间没有将API下放到CAAnimation的唯一方法是将动画分成两半,而不是像这样使用UIViewAnimationOptionAutoreverse :

[UIView animateWithDuration:0.3 delay:i*delay options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationCurveEaseIn animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y-WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:^(BOOL finished) { [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y+WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:NULL]; }];

这确保了在每个动画结束时,视图的模型值(即其当前位置)与动画的最终帧相同。 这意味着您没有获得抖动效果。

这使得UIViewAnimationOptionAutoreverse看起来有点无意义。 如果可以将视图的模型值与动画的最终帧不同,为什么会使用它? 当与UIViewAnimationOptionRepeat结合使用时,它会产生一种相当令人愉悦的效果,它会一直来回移动直到被移除。

This is to do with when the completion block fires with respect to the layer updating from the view's model values, which are set to the middle of the animation. The only way i've found of making this kind of thing work 100% of the time without dropping down an API to CAAnimation is by chaining the animation in two halves as opposed to using UIViewAnimationOptionAutoreverse like so:

[UIView animateWithDuration:0.3 delay:i*delay options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationCurveEaseIn animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y-WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:^(BOOL finished) { [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{ tab.frame = CGRectMake(tab.frame.origin.x, tab.frame.origin.y+WAVE_SIZE, tab.frame.size.width, tab.frame.size.height); } completion:NULL]; }];

This ensures that, at the end of each animation, the view's model values (i.e. its current position) are the same as the animation's final frame. This means that you don't get that jitter effect.

This makes UIViewAnimationOptionAutoreverse seem a bit pointless. Why would one use it if it can leave the model values of the view different to the final frame of the animation? Well when combined with UIViewAnimationOptionRepeat it makes a rather pleasing effect that goes back and forth until removed.

更多推荐

本文发布于:2023-07-26 00:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   UIAnimation   block   behavior   weird

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!