Ios 如何在特定时间重复UIView动画

Ios 如何在特定时间重复UIView动画,ios,uiview,uiviewanimation,Ios,Uiview,Uiviewanimation,我想把这个动画重复三次。 我确认这个代码没有重复 [UIView animateWithDuration:1.0f delay:0.1f options:UIViewAnimationOptionCurveLinear animations:^{ imageOne.center = CGPointMake(100, 150);

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

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

有人能告诉我怎么做吗?
谢谢

int animationLoopCount=0;
-(无效)送达
{
用于(animationLoopCount;animationLoopCount<3;animationLoopCount++){
[UIView animateWithDuration:1.0f
延迟:0.1f
选项:UIViewAnimationOptionCurveLinear
动画:^{
imageOne.center=CGPointMake(100150);
}完成:^(布尔完成){
imageOne.center=CGPointMake(100200);
[自我实现];
如果(animationLoopCount==3)animationLoopCount=0;
}];
}
}
int animationLoopCount=0;
-(无效)送达
{
用于(animationLoopCount;animationLoopCount<3;animationLoopCount++){
[UIView animateWithDuration:1.0f
延迟:0.1f
选项:UIViewAnimationOptionCurveLinear
动画:^{
imageOne.center=CGPointMake(100150);
}完成:^(布尔完成){
imageOne.center=CGPointMake(100200);
[自我实现];
如果(animationLoopCount==3)animationLoopCount=0;
}];
}
}

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

下面是一种通用方法,它使用动画块递归地为事物设置动画

- (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];
    }];
}

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

下面是一种通用方法,它使用动画块递归地为事物设置动画

- (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];
    }];
}
使用这个简单的代码

     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"];
使用这个简单的代码

     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"];

我宁愿做这样的东西:

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];
}

这对我来说更容易理解。

我宁愿做这样的东西:

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];
}

这对我来说更容易理解。

正确的方法是:

[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);
             }];
[UIView animateWithDuration:1.0f
延迟:0.1f
选项:UIViewAnimationOptionCurveLinear
动画:^{

[UIView setAnimationRepeatCount:3.0];//正确的方法如下:

[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);
             }];
[UIView animateWithDuration:1.0f
延迟:0.1f
选项:UIViewAnimationOptionCurveLinear
动画:^{

[UIView setAnimationRepeatCount:3.0]有几种方法。你做过任何研究以找到可能的解决方案吗?有几种方法。你做过任何研究以找到可能的解决方案吗?苹果文档说,iOS 4及更高版本不鼓励使用本节中的方法。改用基于块的动画方法。
苹果文档说
使用方法iOS 4及更高版本不鼓励使用本节中的ds。请改用基于块的动画方法。