2013-02-07 117 views
1

我已經實現了一些很好的代碼,可以根據觸發的定時器自動滾動UIScrollview。在UIScrollview中控制動畫滾動速度 - IOS開發

這就是:

.... 
CGPoint offset; 
.... 
offset = scroller.contentOffset; 
.... 
- (void) scrollWords: (NSTimer *) theTimer 
{ 
offset.y = offset.y+300; 
[UIScrollView beginAnimations:@"scrollAnimation" context:nil]; 
[UIScrollView setAnimationDuration:50.0f]; 
[scroller setContentOffset:offset]; 
[UIScrollView commitAnimations]; 

} 

不過,我已經注意到,滾動正在發生的同時,滾動速率變化;通過它的一半滾動每秒2或3行文本,但在開始和結束時它會慢得多,也許只有每秒0.5行。有沒有辦法控制滾動速度?

在此先感謝。

Paul。

回答

1

您正在查找setAnimationCurve:。具體來說,你所描述的是UIViewAnimationCurveEaseInOut的影響。嘗試添加[UIScrollView setAnimationCurve:UIAnimationCurveLinear];

另外,您正在使用舊式動畫代碼。如果你的目標的iOS 4以上,看看這個新的風格,是更友好的(在我看來):

- (void) scrollWords: (NSTimer *) theTimer 
{ 
    offset.y = offset.y+300; 
    [UIScrollView animateWithDuration:50.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ 
     [scroller setContentOffset:offset]; 
    }]; 
} 

使用延遲參數,你可能甚至擺脫你的NSTimer的。使用此代碼,您可以在5秒後滾動表格視圖。

- (void) scrollWordsLater 
{ 
    offset.y = offset.y+300; 
    [UIScrollView animateWithDuration:50.0f delay:5.0 options:UIViewAnimationOptionCurveLinear animations:^{ 
     [scroller setContentOffset:offset]; 
    }]; 
} 
+0

很好的迴應。謝謝!!!我沒有看到任何不同的使用不同的animationCurves,所以我切換到使用您的代碼示例。似乎有一個問題,我不能解決:我得到這個錯誤消息:「沒有已知的類方法'animateWithDuration:延遲:選項:動畫:'」 –

+0

看起來我遺漏了'完成: NULL'部分!現在很好地工作! :) 謝謝! –

+0

很高興你的工作,並歡迎來到StackOverflow! – wjl