开发者

UIScrollView scroll detection

I have a UIScrollView in a UIViewController view that scrolls horizontally. How can I detect whether the scroll is at the left end or righ开发者_JAVA百科t end or somewhere in the middle?


You will probably need to look in to a scrollViewDelegate method such as below.

ObjC

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"Point: %@", NSStringFromCGPoint(scrollView.contentOffset));
}

Swift

override func scrollViewDidScroll(scrollView: UIScrollView) {
    println(scrollView.contentOffset)
}

Have a look at the apple docs.

Also make sure to set your scrollview delegate your_scroll_view.delegate = self; and your view controller must conform to <UIScrollViewDelegate>


GameBit is correct here, but to elaborate -

The UIScrollView has a member variable contentOffset, that describes how many pixels from the origin the scrollview has scrolled. A positive value is a scroll to the right, negative is a scroll to the left.

Is your UIScrollView in Paged mode? if so this will help:

 CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;


This works for me in horizontal UIScrollView

  • Conform to UIScrollViewDelegate
  • In your ViewController - yourScrollView.delegate = self

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
     var currentPage = yourScrollView.contentOffset.x / yourScrollView.bounds.size.width;
    }
    

Do not use scrollViewDidScroll as you need to wait until scrolling ends

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜