如何在ObjectiveC中为浮动UIView设置动画

Sha*_*amy 2 animation objective-c uiview ios

我试图将UIView动画作为浮动对象,或者作为气球:D UIView位于屏幕中间,我希望它随机围绕其第一个启动点浮动.不是在屏幕上或类似的东西,只是漂浮在它周围5像素的区域.

有什么建议?:d

我试过这个:

[UIView animateWithDuration:0.1
                      delay:0.0
                    options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     myCircleUIView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator], [self randomFloatingGenerator]);
                 }
                 completion:NULL];
Run Code Online (Sandbox Code Playgroud)

randomFloatingGenerator 生成一个介于-5和5之间的数字.问题是,它只执行一次,并且不断重复相同的随机值.

编辑1: 现在我试过这个

-(void)animationLoop{

[UIView animateWithDuration:1.0
                     animations: ^{ myCircleUIView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator], [self randomFloatingGenerator]); }
                     completion:
     ^(BOOL finished) {
         [UIView animateWithDuration:1.0
                          animations:^{ myCircleUIView.transform = CGAffineTransformMakeTranslation(0,0);
                          }
                          completion:
          ^(BOOL finished) {[self animationLoop];}];
     }];
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用,动画是...... Cracky ......我想我正在做一些愚蠢的错误,我需要第二眼来帮助我.

EDIT2: 修正了它.

 -(void)animationLoop{
    CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
    [UIView animateWithDuration:1.0
                     animations: ^{ myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [self randomFloatingGenerator], myCircleUIView.frame.origin.y + [self randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height); }
                     completion:
     ^(BOOL finished) {
         [UIView animateWithDuration:1.0
                          animations:^{ myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);}
                          completion:
          ^(BOOL finished) {[self animationLoop];}];
     }];
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

编辑3:

有人发布了增强的代码.

Seb*_*ddd 5

@ Shamy的解决方案,改进,修复和CPU安全.

-(void)animateFloatView:(UIView*)view{

    // Abort the recursive loop
    if (!view)
        return;

    CGPoint oldPoint = CGPointMake(view.frame.origin.x, view.frame.origin.y);
    [UIView animateWithDuration:0.6
                     animations: ^{ view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - 15, view.frame.size.width, view.frame.size.height); }
                     completion:
     ^(BOOL finished) {

         if (finished) {
             [UIView animateWithDuration:0.6
                              animations:^{ view.frame = CGRectMake(oldPoint.x, oldPoint.y, view.frame.size.width, view.frame.size.height);}
                              completion:
              ^(BOOL finished2) {
                  if(finished2)
                      [self animateFloatView:view];
              }];

         }
     }];
}
Run Code Online (Sandbox Code Playgroud)

无论何时想要停止动画(离开View Controller时都需要),请使用nil调用该函数,如下所示:

    [self recursiveFloatingAnimation:nil];
Run Code Online (Sandbox Code Playgroud)

不停止动画将导致递归循环无限运行并压倒CPU堆栈.