带有自动反转功能的 UIView 动画 [英] UIView animations with autoreverse

查看:23
本文介绍了带有自动反转功能的 UIView 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置 UIViewAnimationOptionAutoReverse 时遇到问题.这是我的代码.

I have a problem with the setting UIViewAnimationOptionAutoReverse. Here is my code.

CALayer *aniLayer = act.viewToChange.layer;
[UIView animateWithDuration:2.0 delay:1.0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse) animations:^{
                    viewToAnimate.frame = GCRectMake(1,1,100,100);
                    [aniLayer setValue:[NSNumber numberWithFloat:degreesToRadians(34)] forKeyPath:@"transform.rotation"];
                } completion:nil ];

问题在于,在动画反转后,视图跳回到动画块中设置的帧.我希望视图增长和不增长"并停止在其原始位置.

The problem is that, after the animation has reversed, the view jumps back to the frame set in the animation block. I want the view to grow and "ungrow" and stop at its original position.

有没有不编写连续两个动画的解决方案?

Is there a solution without programming two consecutive animations?

推荐答案

您有三个选择.

当您使用 -[UIView animateWithDuration:...] 方法时,您在 animations 块中所做的更改会立即应用于相关视图.然而,还有一个隐含的 CAAnimation 应用于视图,从旧值到新值动画.当 CAAnimation 在视图上处于活动状态时,它会更改显示的视图,但不会更改视图的实际属性.

When you use the -[UIView animateWithDuration:…] methods, the changes you make in the animations block are applied immediately to the views in question. However, there is also an implicit CAAnimation applied to the view that animates from the old value to the new value. When a CAAnimation is active on a view, it changes the displayed view, but does not change the actual properties of the view.

例如,如果您这样做:

NSLog(@"old center: %@", NSStringFromCGPoint(someView.center));
[UIView animateWithDuration:2.0 animations: ^{ someView.center = newPoint; }];
NSLog(@"new center: %@", NSStringFromCGPoint(someView.center));

你会看到旧中心"和新中心"是不同的;new center 将立即反映 newPoint 的值.但是,隐式创建的 CAAnimation 将导致视图仍然显示在旧中心并平滑地移动到新中心.动画完成后,它会从视图中移除,然后您切换回仅查看实际模型值.

you will see that 'old center' and 'new center' are different; new center will immediately reflect the values of newPoint. However, the CAAnimation that was implicitly created will cause the view to still be displayed at the old center and smoothly move its way to the new center. When the animation finishes, it is removed from the view and you switch back to just seeing the actual model values.

当您传递 UIViewAnimationOptionAutoreverse 时,它会影响隐式创建的 CAAnimation,但不会影响您对值所做的实际更改.也就是说,如果我们上面的示例定义了 UIViewAnimationOptionAutoreverse,那么隐式创建的 CAAnimation 将从 oldCenter 动画到 newCenter 并返回.然后动画将被移除,我们将切换回查看我们设置的值......仍处于新位置.

When you pass UIViewAnimationOptionAutoreverse, it affects the implicitly created CAAnimation, but does NOT affect the actual change you're making to the values. That is, if our example above had the UIViewAnimationOptionAutoreverse defined, then the implicitly created CAAnimation would animate from oldCenter to newCenter and back. The animation would then be removed, and we'd switch back to seeing the values we set… which is still at the new position.

正如我所说,有三种方法可以解决这个问题.首先是在动画上添加一个完成块来反转它,就像这样:

As I said, there are three ways to deal with this. The first is to add a completion block on the animation to reverse it, like so:

CGPoint oldCenter = someView.center;
[UIView animateWithDuration:2.0
                 animations: ^{ someView.center = newPoint; }
                 completion: 
   ^(BOOL finished) {
       [UIView animateWithDuration:2.0
                        animations:^{ someView.center = oldCenter; }];
   }];

第二个选项

第二个选项是像您正在做的那样自动反转动画,并将视图设置回其在完成块中的原始位置:

Second Option

The second option is to autoreverse the animation like you're doing, and set the view back to its original position in a completion block:

CGPoint oldCenter = someView.center;
[UIView animateWithDuration:2.0
                      delay:0
                    options: UIViewAnimationOptionAutoreverse
                 animations: ^{ someView.center = newPoint; }
                 completion: ^(BOOL finished) { someView.center = oldCenter; }];

但是,这可能会导致动画自动反转完成时间和完成块运行时间之间出现闪烁,因此这可能不是您的最佳选择.

However, this may cause flickering between the time that the animation autoreverse completes and when the completion block runs, so it's probably not your best choice.

最后一个选项是直接创建一个 CAAnimation.当您实际上不想更改要更改的属性的最终值时,这通常更简单.

The last option is to simply create a CAAnimation directly. When you don't actually want to change the final value of the property you're changing, this is often simpler.

#import <QuartzCore/QuartzCore.h>

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.autoreverses = YES;
animation.repeatCount = 1; // Play it just once, and then reverse it
animation.toValue = [NSValue valueWithCGPoint:newPoint];
[someView.layer addAnimation:animation forKey:nil];

请注意,CAAnimation 的处理方式永远不会改变视图的实际值;它只是用动画掩盖了实际值.视图仍然认为它在原始位置.(这意味着,例如,如果您的视图响应触摸事件,它仍然会在原始位置监视这些触摸事件的发生.动画改变了视图的绘制方式;什么也没有否则.

Note that the CAAnimation way of doing things never changes the actual values of the view; it just masks the actual values with an animation. The view still thinks it's in the original location. (This means, for example, that if your view responds to touch events, it will still be watching for those touch events to happen at the original location. The animation only changes the way the view draws; nothing else.

CAAnimation 方式还要求您将其添加到视图的底层 CALayer.如果这让您感到害怕,请随意使用 -[UIView animateWithDuration:...] 方法代替.使用 CAAnimation 可以获得额外的功能,但如果你不熟悉,链接 UIView 动画或在完成块中重置它是完全可以接受的.事实上,这是完成块的主要用途之一.

The CAAnimation way also requires that you add it to the view's underlying CALayer. If this scares you, feel free to use the -[UIView animateWithDuration:…] methods instead. There's additional functionality available by using CAAnimation, but if it's something you're not familiar with, chaining UIView animations or resetting it in the completion block is perfectly acceptable. In fact, that's one of the main purposes of the completion block.

那么,你去吧.反转动画并保持原始值的三种不同方法.享受!

So, there you go. Three different ways to reverse an animation and keep the original value. Enjoy!

这篇关于带有自动反转功能的 UIView 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆