所谓的无限滚动就是把component里的Row设置的足够多
在这确定键是暂停计时器以及滚动,取消键是继续计时器
1.完成如下视图布局
2.为picker添加代理和数据源协议
picker.dataSource = self
picker.delegate = self
//父视图继承相关代理UIPickerViewDelegate,UIPicke rViewDataSource
//设置测试数据(我用的二维数组)
let items = [["a1","a2","a3","a4"],["a1","a2"],["a1","a2","a3"]]
3.完成相关代理方法
// 是否循环滚动
let isCircularly = true
//picker有几组
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}
//每组有多少个
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
for (index,item) in items.enumerated() {
if component == index{
if isCircularly{
return item.count * 1000
}else{
return item.count
}
}
}
return 0
}
//每个显示的内容
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if isCircularly{
return items[component][row % items[component].count]
}else{
return items[component][row]
}
}
这样就可以显示并且无限滑动了
4.老虎机自动滚动
//添加计时器
var time:Timer!
time = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timer), userInfo: nil, repeats: true)
time.fire()
//滚动相关代码
@objc func timer() {
for index in 0..
如果要使各个列滚动方向不同,可以在time()方法里面添加随机事件(+= 或者 -=)
相关计时器的简单用法请看:https://www.jianshu.com/p/185eadc38ff0
git库:https://github.com/Liushaungxi/LearnSwift(List4)