使用nstimer在无限滚动视图中滑动图像

时间:2015-07-20 06:43:55

标签: ios objective-c uiscrollview nstimer

我有三张图片。应该像车轮一样每4秒从右到左动画一次。如果用户拖动到第2张图像,则从第2张到第3张开始滑动4秒后开始。如何实现这个?

我正在使用下面的代码但它没有按预期工作。

   [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(doAnimation) userInfo:nil repeats:YES];

-(void)doAnimation
{

    if (currentPage) {

        [self scrollFromCurrentpage];
        return;
    }

    //CGPoint p=CGPointMake(count*320, 0);

    if (count==3) {
        count=0;

        [_scrollView setContentOffset:CGPointMake(0, 0)animated:NO];

        return;
    }


    count++;
    [UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
     {
         CGRect rect= CGRectMake(count*320, 0, 320, 352);
         [_scrollView scrollRectToVisible:rect animated:NO];

     }completion:^(BOOL finished)
     {


     }];
}
-(void)scrollFromCurrentpage
{

    [UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
     {
         CGRect rect= CGRectMake(currentPage*320, 0, 320, 352);
         [_scrollView scrollRectToVisible:rect animated:NO];
         currentPage=0;

     }completion:^(BOOL finished)
     {


     }];
}

1 个答案:

答案 0 :(得分:0)

NSTimer实例设置属性。您应该实施一个invalidate的方法,并在每次用户手动滚动时重置NSTimer实例。