UIView动画块暂停动画和完成代码

时间:2013-02-27 18:46:05

标签: ios objective-c uiview calayer

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

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详细说明的代码 暂停动画。

-(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;
}

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

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

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

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

1 个答案:

答案 0 :(得分:5)

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

使用UIImageView *transViewUIButton *trigger创建故事板。这是班级:

TSTViewController.h:

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

- (IBAction)didPressTrigger:(id)sender;

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秒钟,然后再次按下按钮。动画继续并结束,并记录“完成”。