1. 程式人生 > 實用技巧 >iOS UIScrollView滾動方法對比

iOS UIScrollView滾動方法對比

專案中我們經常會使用到使檢視滾動的方法,不管是collectionview或者是tableview最終都是使用以下方法

-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;//以恆定速度動畫到新偏移
-(void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;//滾動使rect剛好可見(最近的邊)。如果完全可見就什麼都沒有

但在某些系統下-(void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;方法滾動並不生效。

可以使用-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;方法替代。

//    這個方法在部分系統下不好使 有莫名其妙的問題 時好時不好
//    [self.contentCollectionView scrollRectToVisible:CGRectMake(indexPath.row*LL_ScreenWidth,self.contentCollectionView.frame.origin.y, self.contentCollectionView.frame.size.width, self.contentCollectionView.frame.size.height) animated:NO];
[self.contentCollectionView setContentOffset:CGPointMake(toRow*LL_ScreenWidth, 0) animated:NO];