如何在动画执行时等待

时间:2013-12-27 07:29:16

标签: ios iphone xcode animation

我有两个图像视图,称为动画和指令 动画图像视图播放动画 和显示指令的指令图像视图

我希望在我的动画完成时这样做,然后我想要更改指令图像视图的图像

 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{    

    if(event.subtype == UIEventSubtypeMotionShake) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for(int i=1;i<=20;i++){
            [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"vivacious%d.png",i] ] ];
        }
        self.animation.animationImages = array;
        self.animation.animationRepeatCount = 1;//0 for infinite loop
        self.animation.animationDuration = 1.5;

        [self.animation startAnimating];
        //[NSThread sleepForTimeInterval:1.5];
        self.instruction.image = [UIImage imageNamed:@"ah.png"];
    }
}

4 个答案:

答案 0 :(得分:3)

您可以使用以下方法

[UIView animateWithDuration:5.0
                     animations:^{
                         //code for animation start
                     }
                     completion:^(BOOL finished) {
                         //code for animation end
                     }];

OR 您可以尝试以下方法等待特定时间&amp;然后用选择器

执行方法
[self.view performSelector:@selector(your_Method) withObject:nil afterDelay:5.0];

答案 1 :(得分:0)

您可以在第一个完成后使用带有延迟的选择器,或者甚至可以使用此行,

   [UIView setAnimationDelay:5.0]

这将在延迟5秒后开始动画。

答案 2 :(得分:0)

关于你的代码我想知道你用过什么动画?

1)如果您使用Core Animation,则可以实现CAAnimation的委托。

委托有一个名为- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag的方法,当动画结束时,它将被调用。因此,您可以在其中更改指令图像视图的图像。

但要小心,CAAnimation的代表将保留

2)如果您使用UIView动画,它有一个名为+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion的动画完整块,您可以在完成块中更改指令图像视图的图像

答案 3 :(得分:0)

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{    

    if(event.subtype == UIEventSubtypeMotionShake) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for(int i=1;i<=20;i++){
            [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"vivacious%d.png",i] ] ];
        }

        self.animation.animationImages = array;
        self.animation.animationRepeatCount = 1;//0 for infinite loop
        self.animation.animationDuration = 1.5;

        [self.animation startAnimating];
        self.instruction.hidden = YES;

        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(changeInstruction) userInfo:nil repeats:NO];
    }
}