如何创建动画块IOS

时间:2013-08-27 16:10:47

标签: ios

我有四个UI按钮,我必须更改它们的x原点。到现在为止我正在使用这种方法:

[UIView animateWithDuration:1.0
                          delay:1.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{

                     self.Button1.frame = CGRectMake(-120, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button1.frame.size.height);

                     }
                     completion:^(BOOL finished){

                        [UIView animateWithDuration:1.0
                          delay:1.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{

                     self.Button1.frame = CGRectMake(-140, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button2.frame.size.height);

                     }
                     completion:^(BOOL finished){


                     }];
                     }];

我将一个动画放在下一个的completition部分。但我有4/5按钮。我不能这么做很多次。有没有办法在一个对象上做一个动画,并在延迟0.5秒后在下一个做它?

编辑:

我试着用这个:

typedef void (^AnimationBlock)();

AnimationBlock firstAnimation = ^{ self.Button1.frame = CGRectMake(-120, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button1.frame.size.height); };

AnimationBlock secondAnimation = ^{ self.Button4.frame = CGRectMake(-120, self.Button4.frame.origin.y, self.Button4.frame.size.width, self.Button4.frame.size.height);};

[UIView animateWithDuration:2 animations:firstAnimation completion:^(BOOL finished) {
    [UIView animateWithDuration:2 animations:secondAnimation];
}];

但是没有可能设置两个动画之间的时间。

2 个答案:

答案 0 :(得分:2)

我要做的就是拿走你拥有的代码并使其更通用。然后你可以简单地为任何按钮设置动画。

- (void)animateButton:(UIButton *)button toRect:(CGRect)rect1 thenToRect:(CGRect)rect2 withDelay:(CGFloat)delay
{
    [UIView animateWithDuration:1.0
                          delay:delay
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         button.frame = rect1;
                     }
                     completion:^(BOOL finished){
                        [UIView animateWithDuration:1.0
                                              delay:delay
                                            options:UIViewAnimationCurveEaseInOut
                                         animations:^{
                                             button.frame = rect2;
                                         }
                                         completion:nil];
                     }];
}

然后你就像这样使用它(我简化了rect创建代码以使其更清晰)

[self animateButton:self.button1
             toRect:CGRectOffset(self.button1.frame, -120 - self.button1.frame.origin.x)
         thenToRect:CGRectOffset(self.button1.frame, -140 - self.button1.frame.origin.x)
              delay:0.5f];

答案 1 :(得分:1)

执行类似的操作(在此示例中,我在所有动画中移动相同的UIButton,因为我首先尝试使用XCode,但您可以根据需要更改动画块的内容)

typedef void (^AnimationBlock)();

// declare two example animations
AnimationBlock firstAnimation = ^{
    CGRect firstFrame = self.aButton.frame;
    firstFrame.origin.x = firstFrame.origin.x - 20;
    self.aButton.frame = firstFrame;
};

AnimationBlock secondAnimation = ^{
    CGRect firstFrame = self.aButton.frame;
    firstFrame.origin.x = firstFrame.origin.x - 20;
    self.aButton.frame = firstFrame;
};

// starting point, create an array of animations
NSArray *animations = @[firstAnimation, secondAnimation];

// first animation starts immediately
double delayInSeconds = 0.0;
for (AnimationBlock animation in animations) {

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [UIView animateWithDuration:0.5f animations:[animation copy]];
    });

    // first animation starts immediately, then increase the dispatch_after timer of 0.5 sec at each loop
    delayInSeconds = delayInSeconds + 0.5f;
}

本质:

  • 声明你的动画块,你想要多少个
  • 将其放入数组
  • for循环:在每个循环中,向队列后的分派添加动画。在每个循环中,增加延迟。因此,在循环结束时,您将拥有X动画(与动画数组的数量一样多),所有动画都从前一个开始延迟0.5秒