我发现了几个相关的问题,比如this ,但我仍然需要一点帮助。 我想要的是一个按钮,一旦点击,就会以下列任一方式随机飞行: 1,它飞过五个随机位置并飞回原来的位置。或者 2、随机飞行2秒,然后回到原位。
这是我的代码,但仍然缺少停止控件:
//if the button clicked
[self animationLoop:@"Wrong!" finished:1 context:nil];
-(void)animationLoop:(NSString *)animationID finished:(int)finished context:(void *)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:finished];
CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);
CGPoint squarePostion = CGPointMake(x, y);
theWrongButton.center = squarePostion;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)];
[UIView commitAnimations];
}
我想我的问题有点像如何让动画循环几次或几秒钟。
最佳答案
您要问的正是上下文参数的用途。它使您能够传入以后可以使用的任何对象。例如,您可能正在寻找这样的内容:
[self animationLoop:@"Wrong!" finished:1 context:[NSNumber numberWithInt:0]];
-(void)animationLoop:(NSString *)animationID finished:(int)finished context:(void *)context {
NSNumber *count = (NSNumber *)context;
if ([count intValue]) >= 5) {
return;
}
[UIView beginAnimations:nil context:[NSNumber numberWithInt:[count intValue]+1]];
[UIView setAnimationDuration:1];
[UIView setAnimationRepeatCount:finished];
CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);
CGPoint squarePostion = CGPointMake(x, y);
theWrongButton.center = squarePostion;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)];
[UIView commitAnimations];
}
关于ios - 如何循环动画几次或几秒钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062989/