Change page on UIScrollView

后端 未结 12 1307
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 18:45

I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previ

相关标签:
12条回答
  • 2020-12-04 19:21

    You can do it this way :

    CGRect lastVisibleRect;
    CGSize contentSize = [_scrollView contentSize];
    lastVisibleRect.size.height = contentSize.height; 
    lastVisibleRect.origin.y = 0.0; 
    lastVisibleRect.size.width = PAGE_WIDTH; 
    lastVisibleRect.origin.x  = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index
    
    [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];
    
    0 讨论(0)
  • 2020-12-04 19:23

    Here is a static method in swift:

    static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) {
        var frame: CGRect = scrollView.frame
        frame.origin.x = frame.size.width * CGFloat(page);
        frame.origin.y = 0;
        scrollView.scrollRectToVisible(frame, animated: animated)
    }
    
    0 讨论(0)
  • 2020-12-04 19:24

    Here is an implementation for Swift 4:

    func scrollToPage(page: Int, animated: Bool) {
        var frame: CGRect = self.scrollView.frame
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0
        self.scrollView.scrollRectToVisible(frame, animated: animated)
    }
    

    and is easily invoked by calling:

    self.scrollToPage(1, animated: true)
    

    Edit:

    A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:

    extension UIScrollView {
    
        func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true) {
            var frame: CGRect = self.frame
            frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
            frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
            self.scrollRectToVisible(frame, animated: animated ?? true)
        }
    
    }
    

    This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.

    self.scrollView.scrollTo(horizontalPage: 0)
    self.scrollView.scrollTo(verticalPage: 2, animated: true)
    self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)
    
    0 讨论(0)
  • 2020-12-04 19:26
    scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
    
    0 讨论(0)
  • 2020-12-04 19:30

    For Swift 3 this is an extension that I find very convenient:

    extension UIScrollView {
        func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval) {
            let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
            DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
                self.setContentOffset(offset, animated: animated)
            })
        }
    }
    

    And you call it like this:

    scrollView.scrollToPage(index: 1, animated: true, after: 0.5)
    
    0 讨论(0)
  • 2020-12-04 19:34

    You just tell the buttons to scroll to the page's location:

    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    
    0 讨论(0)
提交回复
热议问题