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

标签 ios animation loops time

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

相关文章:

iphone - 应用拒绝 : Google Analytics ios sdk 2. 0

python - Kivy:用 Canvas 制作动画图像的正确方法是什么?

javascript - 禁止在动画期间单击某个元素

javascript - 如何使用 javascript 循环 JSON 响应

javascript - 如何通过递归减少传播

java - 将计算值存储到数组中

ios - 如何本地化 Cordova iOS 项目?

ios - Swift UITableView 单元格选择在搜索结果后不起作用

iphone - 如何将字符串从一个 uiviewcontroller 传递到另一个

css - 关键帧动画不适用于 SVG...尝试使用变换将 child 向上、向右、向下移动一个 Angular ,然后向后移动。这可能吗?