使UIScrollView像UIPageController一样滚动

时间:2016-03-29 19:48:50

标签: ios swift

有没有什么方法可以像UIPageController一样制作UIScrollView滚动或像页面一样,因为目前当我们滚动UIScrollView时它就像免费一样..

2 个答案:

答案 0 :(得分:2)

如上所述,作为Heximal,scrollview具有属性pagingEnabled,您需要启用它以在scrollview中进行分页

您的示例代码可以是这样的:

@IBOutlet weak var scrollPagination: UIScrollView!
var numberOfPages:CGFloat = 5

override func viewDidLoad() {

    super.viewDidLoad()

    scrollPagination.pagingEnabled = true
    scrollPagination.contentSize = CGSizeMake(numberOfPages * scrollPagination.frame.size.width, scrollPagination.frame.size.height)

    for i in 0...Int(numberOfPages) {
        let tmpLabel: UILabel = UILabel(frame: CGRectMake(CGFloat(i) * self.view.bounds.width + 20, 20, 120, 20))
        tmpLabel.textAlignment = .Center
        tmpLabel.text = "This is page \(i)"
        scrollPagination.addSubview(tmpLabel)
    }
}

代码的另一个代码参考:https://stackoverflow.com/a/29300300/4557505

答案 1 :(得分:0)

是的,UIScrollView具有您正在寻找的功能。看here