[英]how to wait while animation is performing
我有两个图像视图,分别称为动画和指令动画图像视图(用于播放动画)和指令图像视图(用于显示指令)
我想在动画结束时执行此操作,然后更改指令图像视图的图像
-(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"];
}
}
您可以使用以下方法
[UIView animateWithDuration:5.0
animations:^{
//code for animation start
}
completion:^(BOOL finished) {
//code for animation end
}];
或者您可以尝试以下方法等待特定时间,然后使用选择器执行方法
[self.view performSelector:@selector(your_Method) withObject:nil afterDelay:5.0];
您可以在第一个选择完成后延迟使用选择器,甚至可以使用此行,
[UIView setAnimationDelay:5.0]
延迟5秒后,将开始播放动画。
关于您的代码,我想知道您使用了哪种动画?
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
,可以在完成块中更改指令图像视图的图像
-(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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.