Change page on UIScrollView
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 previous or next page. I can't seem to figure out a way to do this though. A lot of my code comes from Apple's page control sample code. Can anybod开发者_JAVA百科y help?
Thanks
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];
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)
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.
func scrollToPage(_ page: Int) {
UIView.animate(withDuration: 0.3) {
self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)
}
}
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)
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];
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)
}
First create a UIScrollView extension like:
extension UIScrollView {
func setCurrentPage(position: Int) {
var frame = self.frame;
frame.origin.x = frame.size.width * CGFloat(position)
frame.origin.y = 0
scrollRectToVisible(frame, animated: true)
}
}
then just call:
self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page
If scrollRectToVisible doesn't work try this :
let frame = scrollView.frame
let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
self.scrollView.setContentOffset(offset, animated: true)
I wanted to implement an image slider with automatic scrolling using a UIScrollView and a UIStackView. This is what I ended up with and it's working pretty well. I a using PureLayout for AutoLayout:
class ImageSlideShow: UIView {
private let scrollView = UIScrollView()
private var images: [UIImage] = []
private var scrollTimer: Timer?
init() {
super.init(frame: .zero)
self.setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func setImages(_ images: [UIImage], initialIndex: Int = 0) {
self.images = images
self.scrollTimer?.invalidate()
self.scrollView.removeAllSubviews()
let stackView = self.getStackView()
self.scrollView.addSubview(stackView)
stackView.autoMatch(.height, to: .height, of: self.scrollView)
stackView.autoPinEdgesToSuperviewEdges()
self.images.forEach { image in
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
stackView.addArrangedSubview(imageView)
imageView.autoMatch(.width, to: .width, of: self.scrollView)
}
self.setupScrollTimer()
}
private func getStackView() -> UIStackView {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .fill
stackView.distribution = .equalSpacing
stackView.spacing = 0
return stackView
}
private func setupScrollTimer() {
self.scrollTimer = Timer.scheduledTimer(
timeInterval: 0.5,
target: self,
selector: #selector(scroll), userInfo: nil,
repeats: true
)
}
@objc private func scroll() {
let currentPage = self.scrollView.contentOffset.x / scrollView.frame.size.width
var nextPage = currentPage + 1
var frame: CGRect = self.scrollView.frame
if nextPage >= CGFloat(self.images.count) {
nextPage = 0
}
frame.origin.x = frame.size.width * CGFloat(nextPage)
self.scrollView.scrollRectToVisible(frame, animated: true)
}
private func setupView() {
self.backgroundColor = Colors.cream
self.setupScrollView()
}
private func setupScrollView() {
self.addSubview(self.scrollView)
self.scrollView.autoPinEdgesToSuperviewEdges(with: .margin(Metrics.paddingDoublePlus))
self.scrollView.isPagingEnabled = true
self.scrollView.layer.cornerRadius = Metrics.cornerRadius
self.scrollView.showsHorizontalScrollIndicator = false
self.scrollView.showsVerticalScrollIndicator = false
}
}
Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
Swift Solution
func changePage(pageControl: UIPageControl) {
let page = CGFloat(pageControl.currentPage)
var frame = self.scrollView.frame
frame.origin.x = frame.size.width * page
frame.origin.y = 0
self.scrollView.scrollRectToVisible(frame, animated: true)
}
精彩评论