Change page on UIScrollView

后端 未结 12 1343
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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)
    

提交回复
热议问题