一个可定制的、易于使用的无限滚动视图,类似于 App Store 横幅。
GKAutoScrollingView 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "GKAutoScrollingView"
查看演示。
1: 将 AutoScrollingView
添加到您的 UIViewController
。
2: 设置 dataSource
和 delegate
(可选)
class ViewController: UIViewController, AutoScrollingViewDataSource, AutoScrollingViewDelegate {
var autoScrollView: AutoScrollingView!
override func viewDidLoad() {
super.viewDidLoad()
autoScrollView = AutoScrollingView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
autoScrollView.dataSource = self
autoScrollView.delegate = self
self.view.addSubview(autoScrollView)
}
}
3: 遵守 AutoScrollingView
的 dataSource
extension MyViewController: AutoScrollingViewDataSource{
func createImageViews()->[UIImageView]{
var array: [UIImageView] = []
for index in 0...3 {
let img = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300))
img.image = UIImage(named: "\(index)")
array.append(img)
}
return array
}
func setAutoScrollingViewDataSource(autoScrollingView: AutoScrollingView) -> [UIView] {
return createImageViews()
}
}
4: 遵守 AutoScrollingView
的 delegate
(所有都是可选的)
extension MyViewController: AutoScrollingViewDelegate{
func autoScrollingView(autoScrollingView: AutoScrollingView, didSelectItem index: Int) {
print("did select")
}
func autoScrollingView(autoScrollingView: AutoScrollingView, didChangeStatus status: ScrollingState) {
print(status.rawValue)
}
func autoScrollingView(autoScrollingView: AutoScrollingView, didAutoScroll index: Int) {
print("auto scrolling at index ", index)
}
}
返回用于 ScrollView
的 UIView
数组
func setAutoScrollingViewDataSource(autoScrollingView: AutoScrollingView)->[UIView]
返回所选视图的索引
optional func autoScrollingView(autoScrollingView: AutoScrollingView, didSelectItem index: Int)
在视图滚动之前返回 currentItem
的索引
optional func autoScrollingView(autoScrollingView: AutoScrollingView, didAutoScroll index: Int)
返回滑动自动化状态的 AutoScrollingView
。(未暂停
或 暂停
optional func autoScrollingView(autoScrollingView: AutoScrollingView, didChangeStatus status: ScrollingState)
停止 ScrollView
的自动化
pauseScrolling()
开始滚动自动化(如果滚动已暂停)
startScrolling()
设置 autoScrolling
之间的间隔。默认是 2.0
秒。
public var timerInterval: Double!
GKAutoScrollingView 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。