UIView动画块暂停动画和完成代码 [英] UIView animation block pause both the animation and the completion code

查看:113
本文介绍了UIView动画块暂停动画和完成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动画块来执行一个简单的基于变换的动画,完成时会从其超级视图中删除有问题的视图。

I have an animation block to perform a simple transform based animation, that on completion removes the view in question from its superview.

UIView *msgView = [[UIView alloc] initWithFrame:CGRectMake(160, 120, 160, 100)];

// Do stuff to set up the subviews of msgView.

// Add the msgView to the superview (ViewController) that is going to display it.

CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);

[UIView animateWithDuration:5.0 
                 animations:^(void){msgView.layer.transform = transform;}
                 completion:^(BOOL finished){[msgView removeFromSuperview];}];

然后我使用Tech Q& A 1673详述的代码http://developer.apple.com/library/ios/#qa/qa1673/_index.html
暂停动画。

I then use the code as detailed by Tech Q&A 1673 http://developer.apple.com/library/ios/#qa/qa1673/_index.html to pause the animation.

-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

但是,此代码不会阻止执行完成代码。因此,为了防止代码执行,我将完成代码更改为:

However, this code does not prevent the completion code from executing. So to prevent the code from executing I change the code for completion to this:

completion:^(BOOL finished){if(finished == TRUE)[msgView removeFromSuperview];};

检查完成时== TRUE会阻止在动画块暂停时执行完成代码。如果在取消暂停动画之前现在超出持续时间,则不会执行完成代码。即在这种情况下,msgView仍保留在超级视图中。

Whilst checking for finished == TRUE prevents the completion code from being executed whilst the animation block is paused. If the duration time is now exceded before you "unpause" the animation, the completion code will not be executed. ie In this case the msgView remains in the superview.

无论如何暂停/取消同步动画和与完成代码相关联的计时器(如果这是正在发生的事情) )?

Is there anyway to pause/unpause both the animation and the timer associated with the completion code (if that's what's going on)?

推荐答案

正如我在上面的评论中所说,似乎没有问题。以下内容在iOS 5.1和6.1中进行了测试。

As I said in the comments above, there doesn't seem to be a problem. The below is tested in iOS 5.1 and 6.1.

创建一个带有 UIImageView * transView 和<$ c $的故事板c> UIButton *触发器。这是班级:

Create a storyboard with UIImageView *transView and UIButton *trigger. Here's the class:

TSTViewController.h:

TSTViewController.h:

@property (weak, nonatomic) IBOutlet UIImageView *transView;
@property (weak, nonatomic) IBOutlet UIButton *trigger;
@property (nonatomic) NSUInteger bState;

- (IBAction)didPressTrigger:(id)sender;

TSTViewController.m:

TSTViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.bState = 0;    // 0 is initial state
                        // 1 is transform being animated
                        // 2 is transform paused
                        // 3 is transform ended
}

- (IBAction)didPressTrigger:(id)sender {
    switch (self.bState) {
        case 0:
        {
            CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);
            self.bState++;
            [UIView animateWithDuration:5.0
                             animations:^(void){self.transView.layer.transform = transform;}
                             completion:^(BOOL finished){
                                 self.bState = 3;
                                 NSLog(@"Done");
                             }];
            break;
        }
        case 1:
        {
            self.bState++;
            [self pauseLayer:self.transView.layer];
            break;
        }
        case 2:
        {
            self.bState = 1;
            [self resumeLayer:self.transView.layer];
            break;
        }
        case 3:
        {
            [UIView animateWithDuration:0 animations:^(void){self.transView.layer.transform = CATransform3DIdentity;}
                             completion:^(BOOL finished) {
                                 self.bState = 0;
                                 NSLog(@"Reset");
                             }];
            break;
        }
        default:
            break;
    }
}

-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

按下触发按钮时,动画开始播放。再按一次,动画停止。等待10秒钟,然后再次按下按钮。动画继续并结束,并记录完成。

When you press the trigger button, the animation starts. Press it again, the animation stops. Wait 10 seconds, and press the button yet again. The animation continues and finishes, and it logs "Done".

这篇关于UIView动画块暂停动画和完成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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