在UIScrollView上更改页面

时间:2009-12-18 07:16:54

标签: iphone uiscrollview

我有一个10页的UIScrollView。我能够在他们之间轻弹。我还希望有2个按钮(后退按钮和下一个按钮),当触摸时将转到上一页或下一页。我似乎无法找到一种方法来做到这一点。我的很多代码来自Apple的页面控制示例代码。有人可以帮忙吗?

由于

12 个答案:

答案 0 :(得分:133)

您只需告诉按钮滚动到该页面的位置:

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

答案 1 :(得分:18)

scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);

答案 2 :(得分:16)

以下是 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)
}

可以通过调用

轻松调用
self.scrollToPage(1, animated: true)

编辑:

更好的方法是支持横向和纵向分页。这是一个方便的扩展:

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)
    }

}

这会在UIScrollView上创建一个扩展,您可以在其中滚动到任何页面,垂直或水平。

self.scrollView.scrollTo(horizontalPage: 0)
self.scrollView.scrollTo(verticalPage: 2, animated: true)
self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)

答案 3 :(得分:13)

scrollRectToVisible对我不起作用,所以我不得不为contentOffset设置动画。此代码适用于swift 3。

func scrollToPage(_ page: Int) {
    UIView.animate(withDuration: 0.3) { 
        self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)
    }
}

答案 4 :(得分:7)

对于 Swift 3 ,这是我觉得非常方便的扩展程序:

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)
        })
    }
}

你这样称呼它:

scrollView.scrollToPage(index: 1, animated: true, after: 0.5)

答案 5 :(得分:2)

你可以这样做:

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];

答案 6 :(得分:2)

以下是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)
}

答案 7 :(得分:2)

首先创建一个UIScrollView扩展名,如:

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)
    }

}

然后打电话:

self.scrollView.setCurrentPage(position: 2)  // where 2 is your desired page

答案 8 :(得分:0)

如果scrollRectToVisible不起作用,请尝试:

let frame = scrollView.frame
    let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
    self.scrollView.setContentOffset(offset, animated: true)

答案 9 :(得分:0)

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)
}

答案 10 :(得分:0)

我想使用UIScrollView和UIStackView通过自动滚动实现图像滑块。这就是我最终得到的结果,并且运行良好。我将PureLayout用于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
}

}

答案 11 :(得分:-1)

除了添加了 mjdth 的代码之外,请记住将其放在 viewWillAppear viewDidAppear 中。

CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];