2013-10-08 129 views
4

我試圖用scrollViewWillEndDragging:方法來推出自己的分頁UICollectionView與類似App Store應用兩側預覽。然而,如果我停止拖動沒有慣性(即,只是舉起我的手指),使用scrollview下方的代碼將只滾動到所需的矩形。如果我的方法仍然被調用的慣性任何量輕彈,但滾動視圖不滾動所需的矩形,它只是不斷滾動?UIScrollView委託方法scrollViewWillEndDragging:不工作?

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
        withVelocity:(CGPoint)velocity 
       targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    int itemWidth = 280; 

    MyCollectionView *collectionView = (MyIndexedCollectionView *) scrollView; 

    int totalPages = [self.colorArray[collectionView.index] count]; 
    int currentPage; 
    int nextPage; 

    if (lastContentOffset < (int)scrollView.contentOffset.x) 
    { 
     // moved right 
     NSLog(@"scrolling right"); 
     double currentPageOffset = ceil((scrollView.contentSize.width - 
               scrollView.contentOffset.x)/itemWidth); 
     currentPage = totalPages - currentPageOffset; 
     nextPage = currentPage >= totalPages ? totalPages : currentPage + 1; 
    } 
    else if (lastContentOffset > (int)scrollView.contentOffset.x) 
    { 
     // moved left 
     NSLog(@"scrolling left"); 
     double currentPageOffset = floor((scrollView.contentSize.width - 
               scrollView.contentOffset.x)/itemWidth); 
     currentPage = totalPages - currentPageOffset; 
     nextPage = currentPage <= 0 ? 0 : currentPage - 1; 
    } 

    int xOffset = (nextPage * itemWidth); 
    int nextOffsetPage = (totalPages - ((scrollView.contentSize.width - xOffset)/
                     itemWidth)) + 1; 

    [scrollView scrollRectToVisible:CGRectMake(xOffset, 
              0, 
              collectionView.bounds.size.width, 
              collectionView.bounds.size.height) 
              animated:YES]; 
} 

這種方法放棄後,我試着用scrollViewWillBeginDecelerating:方法,而不是和完全相同的代碼工作完美?爲什麼我期待使用scrollViewWillEndDragging:方法,而不是其原因是出於兩個原因:

  • 我想調整它跳到基於滾動速度的項目
  • scrollViewWillBeginDecelerating:方法不如果你沒有任何慣性地舉起你的手指停止拖動就會被叫。

任何想法,我誤解了什麼這個回調是在幹什麼?

回答

1

DOH !!事實證明,我應該一直在使用targetContentOffset設置偏移我想滾動的,而不是scrollToRectToVisible: ALA:

*targetContentOffset = CGPointMake(myTargetOffset, targetContentOffset->y); 

RTFM :)