您可以在 Objective-C 版本的完整演示中找到:https://github.com/ltebean/LTInfiniteScrollView。
pod 'LTInfiniteScrollViewSwift'
或者直接将 LTInfiniteScrollView.swift
拷贝到您的项目中。
您可以通过以下方式创建滚动视图:
scrollView = LTInfiniteScrollView(frame: CGRect(x: 0, y: 200, width: screenWidth, height: 300))
scrollView.dataSource = self
scrollView.delegate = self
scrollView.maxScrollDistance = 5
scrollView.reloadData(initialIndex: 0)
然后实现 LTInfiniteScrollViewDataSource
协议
public protocol LTInfiniteScrollViewDataSource: class {
func viewAtIndex(index: Int, reusingView view: UIView?) -> UIView
func numberOfViews() -> Int
func numberOfVisibleViews() -> Int
}
示例代码
func numberOfViews() -> Int {
return 100
}
func numberOfVisibleViews() -> Int {
return 5
}
func viewAtIndex(index: Int, reusingView view: UIView?) -> UIView {
if let label = view as? UILabel {
label.text = "\(index)"
return label
}
else {
let size = screenWidth / CGFloat(numberOfVisibleViews())
let label = UILabel(frame: CGRect(x: 0, y: 0, width: size, height: size))
label.textAlignment = .Center
label.backgroundColor = UIColor.darkGrayColor()
label.textColor = UIColor.whiteColor()
label.layer.cornerRadius = size / 2
label.layer.masksToBounds = true
label.text = "\(index)"
return label
}
}
如果您希望在滚动过程中应用任何动画,实现 LTInfiniteScrollViewDelegate
协议
public protocol LTInfiniteScrollViewDelegate: class {
func updateView(view: UIView, withProgress progress: CGFloat, scrollDirection direction: ScrollDirection)
}
进度值的范围取决于该视图的位置;如果有 5 个可见视图,则值的范围在 -2 到 2 之间
| |
|-2 -1 0 1 2|
| |
您可以克隆项目并查看示例以获取详细信息。