您可以在 Objective-C 版本中找到完整的演示:https://github.com/ltebean/LTInfiniteScrollView。
由于在使用框架时存在重大的性能问题,我建议您只需将 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)
}
progress 的值取决于该视图的位置,如果有 5 个可见视图,值将在 -2 到 2 之间
| |
|-2 -1 0 1 2|
| |
您可以通过克隆项目来查看示例以了解更多详情。