2012-07-19 72 views
22

我希望UIScrollView在快速掃描後快速滾動,like this。 雖然打開頁面時,滾動工作one page at a time。 當使用手指輕拂啓用分頁而不使用手勢識別器進行手動實現時,是否可以快速滾動?帶分頁的UIScrollView的靈敏度/滾動速度

+0

有沒有找到解決方案呢? – Eric 2012-08-29 22:45:31

+0

我希望有一個很好的解決方案。看起來有兩種實現方法:手動實現手勢識別器或手動實現分頁。 – UrK 2012-08-30 05:29:26

+0

是的這可能是我在6個月前實施的。你可以手動做手勢識別器。 – JohnWhite 2012-09-17 08:43:44

回答

40

這很簡單,但是您必須自己實現分頁方面(這非常簡單)。你不需要手勢識別器。

斯威夫特

首先,調整的UIScrollView減速率:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast 

假設我們有內容的陣列 - 姑且稱之爲yourPagesArray。在您的UIScrollViewDelegate,實現以下方法:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{ 
    //This is the index of the "page" that we will be landing at 
    let nearestIndex = Int(CGFloat(targetContentOffset.pointee.x)/scrollView.bounds.size.width + 0.5) 

    //Just to make sure we don't scroll past your content 
    let clampedIndex = max(min(nearestIndex, yourPagesArray.count - 1), 0) 

    //This is the actual x position in the scroll view 
    var xOffset = CGFloat(clampedIndex) * scrollView.bounds.size.width 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset == 0.0 ? 1.0 : xOffset 

    //Tell the scroll view to land on our page 
    targetContentOffset.pointee.x = xOffset 
} 

您還需要實現以下的委託方法,來處理情況:用戶輕輕地提起他們的手指,而不會造成任何滾動減速:

(更新:你可能不需要這麼做了最新的iOS SDK下好像當出現零速度上面的委託方法現在被稱爲)

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) 
{ 
    if !decelerate 
    { 
     let currentIndex = floor(scrollView.contentOffset.x/scrollView.bounds.size.width) 

     let offset = CGPoint(x: scrollView.bounds.size.width * currentIndex, y: 0) 

     scrollView.setContentOffset(offset, animated: true) 
    } 
} 

Objective-C的

設置你的減速率:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast; 

然後滾動視圖的委託實現:

- (void) scrollViewWillEndDragging:(UIScrollView *)scroll withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    //This is the index of the "page" that we will be landing at 
    NSUInteger nearestIndex = (NSUInteger)(targetContentOffset->x/scrollView.bounds.size.width + 0.5f); 

    //Just to make sure we don't scroll past your content 
    nearestIndex = MAX(MIN(nearestIndex, yourPagesArray.count - 1), 0); 

    //This is the actual x position in the scroll view 
    CGFloat xOffset = nearestIndex * scrollView.bounds.size.width; 

    //I've found that scroll views will "stick" unless this is done 
    xOffset = xOffset==0?1:xOffset; 

    //Tell the scroll view to land on our page 
    *targetContentOffset = CGPointMake(xOffset, targetContentOffset->y); 
} 

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    if(!decelerate) 
    { 
     NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x/scrollView.bounds.size.width); 

     [scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES]; 
    } 
} 
1

我認爲,我們可以設置scrollView.userInteractionEnabled = NO,當我們的手指觸摸它。然後,當動畫停止時,打開它,它適用於我。 希望它能幫助你。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 
    scrollView.userInteractionEnabled = NO; 
} 

// at the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    scrollView.userInteractionEnabled = YES; 
}