3

我有四个 UIbuttons,我必须更改它们的 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){


                     }];
                     }];

我将一个动画放在下一个的完成部分。但我有 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];
}];

但无法设置两个动画之间的时间。

4

2 回答 2

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];
于 2013-08-27T16:27:36.113 回答
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 秒开始
于 2013-08-27T16:34:47.167 回答