gpt4 book ai didi

ios - 如何循环动画几次或几秒钟?

转载 作者:行者123 更新时间:2023-11-29 13:22:28 25 4
gpt4 key购买 nike

我发现了几个相关的问题,比如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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com