2012-11-07 143 views
0

我設置了一些代碼,在TimeInterval後自動滾動(分頁)滾動視圖。現在我想在4次後停止scrollview動畫。有人可以教我如何做到這一點?在UIScrollview上停止動畫

這是我的代碼。

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

- (void) onTimer { 

    // Updates the variable h, adding 320 
    abc += 320; 

    //This makes the scrollView scroll to the desired position 
    [UIView animateWithDuration:1.5f animations:^{ 
     [scrollView setContentOffset:CGPointMake(abc, 0) animated:NO]; 
     }]; 
} 

回答

2

首先添加伊娃到一個NSTimer

{ 
    NSTimer *timer; 
} 

timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

然後在onTimer

- (void) onTimer { 
    //Create a static int 
    static int repetition = 0; 
    repetition ++; 
    if(repetition == 4) 
    { 
     repetition = 0; 
     //Stop the timer 
     [timer invalidate]; 
    } 

    // Updates the variable h, adding 320 
    abc += 320; 

    //This makes the scrollView scroll to the desired position 
    [UIView animateWithDuration:1.5f animations:^{ 
     [scrollView setContentOffset:CGPointMake(abc, 0)animated:NO]; 


    }]; 
} 
+0

我說我scheduledtimer到視圖做負載!那可能嗎?對我來說,它似乎工作......?! – MasterRazer