如何重复UIView动画特定时间

时间:2014-07-29 03:46:44

标签: ios uiview uiviewanimation

我想重复这个动画三次 我确认这段代码没有重复。

[UIView animateWithDuration:1.0f
                      delay:0.1f
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     imageOne.center = CGPointMake(100, 150);
                 } completion:^(BOOL finished) {
                     imageOne.center = CGPointMake(100,200);
                 }];


有人会告诉我该怎么做吗? 谢谢!

5 个答案:

答案 0 :(得分:3)

int animationLoopCount = 0;
- (void)doAnimation
{
    for (animationLoopCount; animationLoopCount < 3; animationLoopCount++) {
    [UIView animateWithDuration:1.0f
                      delay:0.1f
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     imageOne.center = CGPointMake(100, 150);
                 } completion:^(BOOL finished) {
                     imageOne.center = CGPointMake(100,200);
                     [self doAnimation];
                     if (animationLoopCount == 3) animationLoopCount = 0;
                 }];
    }
}

答案 1 :(得分:3)

有几种方法可以实现这一点,我认为最干净的方法可能是使用递归。

这是一个通用的方法,用动画块递归动画。

- (void) animateWithDuration:(NSTimeInterval) duration
                       delay:(NSTimeInterval) delay
                     options:(UIViewAnimationOptions) options
                  animations:(void(^)( )) animations
                  completion:(void(^)( BOOL finished )) completion
                  repeatCount:(NSInteger) repeatCount {
    // Only do the animation if we have an animations block, and our count is bigger than 0.
    if ( repeatCount <= 0 || !animations )
        return;

    // Do the animation
    [UIView animateWithDuration: duration
                          delay: delay
                        options: options
                     animations:^{
                         // Invoke the animations block
                         animations();
                     }
                     completion:^(BOOL finished) {
                         // Invoke the animation completion if it exists
                         if ( completion )
                             completion( finished );

                         // Invoke ourself again with a decremented repeat count.
                         [self animateWithDuration: duration
                                             delay: delay
                                           options: options
                                        animations: animations
                                        completion: completion
                                       repeatCount: repeatCount - 1];
    }];
}

以下是使用您提供的示例在代码中使用它的方法。

[self animateWithDuration: 1.0f
                    delay: 0.1f
                  options: UIViewAnimationOptionCurveLinear
               animations: ^{
                   imageOne.center = CGPointMake(100, 150);
               }
               completion: ^(BOOL finished) {
                   imageOne.center = CGPointMake(100,200);
               }
              repeatCount: 3];

答案 2 :(得分:2)

这样做的正确方法是这样的:

[UIView animateWithDuration:1.0f
                  delay:0.1f
                options:UIViewAnimationOptionCurveLinear
             animations:^{
                 [UIView setAnimationRepeatCount:3.0];// <===
                 imageOne.center = CGPointMake(100, 150);
             } completion:^(BOOL finished) {
                 imageOne.center = CGPointMake(100,200);
             }];

答案 3 :(得分:0)

使用这个简单的代码

     CABasicAnimation* animate =  [CABasicAnimation animationWithKeyPath:@"position"];
     [animate setDuration:1.0];
     [animate setRepeatCount:3];
     [animate setAutoreverses:YES];

     [animate setFromValue:[NSValue valueWithCGPoint:
                          CGPointMake(imageOne.center.x, imageOne.center.y)]];
     [animate setToValue:[NSValue valueWithCGPoint:
                        CGPointMake(imageOne.center.x, imageOne.center.y+100)]];
     [line.layer addAnimation:animate forKey:@"position"];

答案 4 :(得分:0)

我宁愿做这样的事情:

const CGFloat AnimationDuration = 1.0;
const CGFloat AnimationDelay = 0.1;
const NSUInteger AnimationCount = 3;

- (void) someAction
{
    [self showAnimationTimes:AnimationCount];
}

- (void) showAnimationTimes:(NSUInteger)count
{
    __block NSUInteger blockCount = count;

    [self animation:^
     {
         imageOne.center = CGPointMake(100, 150);
     }
         completion:^(BOOL finished)
     {
         imageOne.center = CGPointMake(100, 200);

         if (count > 1)
         {
             [self showAnimationTimes:--blockCount];
         }
     }];
}

- (void) animation:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:AnimationDuration
                          delay:AnimationDelay
                        options:UIViewAnimationOptionCurveLinear
                     animations:animations
                     completion:completion];
}

对我来说更容易理解。