xcode,UIScrollView保持循环滚动动画

时间:2014-08-15 09:59:02

标签: ios objective-c uiscrollview

目标

  • 尝试制作滚动视图,它可以自动滚动到左侧。完成后将自动滚动到右侧。并重复。

滚动动画保持循环并继续保持" AfterDone"方法甚至动画尚未完成。

- (void) afterAnimationStops{
float newOffSetX = _tableview.bounds.size.width-wContentBgImg;
NSLog(@"Inside AutoMove > %d",LzAutoMove);
if(leftright==1 && LzAutoMove==0)
{
    NSLog(@"Come In > 1");
    LzAutoMove = 1;
    leftright = 2;
    [UIScrollView animateWithDuration:3.0f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
                         [UIScrollView setAnimationDuration:3.0f];
                         LzcurrentLocation = (-newOffSetX);
                         [imgCell.scrollview setContentOffset:CGPointMake((-newOffSetX), 0)];
                         [UIScrollView commitAnimations];
                     }
                     completion:^(BOOL finished){
                         if (finished) {
                             [self afterDone];
                         }
                     }];
}else{
    if(LzAutoMove==0){
    NSLog(@"Come In > 2");
        LzAutoMove = 1;
        leftright = 1;
        [UIScrollView animateWithDuration:3.0f
                                    delay:0.0f
                                  options:UIViewAnimationOptionCurveEaseOut
                               animations:^{
                                   [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
                                   [UIScrollView setAnimationDuration:3.0f];
                                   LzcurrentLocation = 0;
                                   [imgCell.scrollview setContentOffset:CGPointMake(0, 0)];
                                   [UIScrollView commitAnimations];
                                   //[UIScrollView setAnimationDidStopSelector:@selector(afterDone)];
                               }
                               completion:^(BOOL finished){
                                   if (finished) {
                                       [self afterDone];
                                   }
                               }];

    }

}
}

- (void) afterDone{
    NSLog(@"Done");
    LzAutoMove = 0;
    [self afterAnimationStops];
}

我还在学习如何使用"动画完成"方法,是任何人都可以告诉我我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse标记作为动画选项,动画将在每次完成后自动重复并反转为原始状态。

[UIView animateWithDuration:3.0f
                            delay:0.0f
                          options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                       animations:^{
                           [imgCell.scrollview setContentOffset:contentOffset];
                       }
                       completion:^(BOOL finished){
                       }];