我在iOS中使用Swift 3制作了一些财富之轮。我使用了带有UIPickerView的库,我自定义了视图,我使行无穷无尽(因此用户有了滚动浏览的想法)无尽的物品)。
唯一的问题是,用户可以通过命运之轮的价格缓慢滑动,所以基本上他可以选择他想要的任何价格。
我虽然要防止这种情况,但无论何时用户点击或滑动滚轮,我都会使UIPickerView旋转并使用随机数来决定命运之轮的结果。
我知道如何在方向盘上添加手势识别器,但我唯一不知道的是如何以编程方式启动UIPickerView旋转。
提前致谢!
答案 0 :(得分:1)
我认为你正在寻找这个:
pick.selectRow(10, inComponent: 0, animated: true)
但如果你想看一个完整的例子,我就是为你做的。
class ViewController: UIViewController, UIPickerViewDataSource{
@IBOutlet var pick: UIPickerView!
var myArr = [Int]()
var myT = Timer()
override func viewDidLoad() {
super.viewDidLoad()
for element in 0...100 {
myArr.append(element)
}
myT = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(ViewController.movePicker), userInfo: nil, repeats: true)
}
//MARK: - move picker
func movePicker() {
let position = Int(arc4random_uniform(89) + 10)
pick.selectRow(position, inComponent: 0, animated: true)
pick.showsSelectionIndicator = true
if position == 50 || position == 72 || position == 90 || position == 35 {
myT.invalidate()
let alert = UIAlertController(title: "You Won!!", message: "Congratulations!!!", preferredStyle: .alert)
let buttonOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
let playAgain = UIAlertAction(title: "Play Again!", style: .default, handler: { (action) in
self.myT = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(ViewController.movePicker), userInfo: nil, repeats: true)
})
alert.addAction(buttonOK)
alert.addAction(playAgain)
present(alert, animated: true, completion: nil)
}
}
//MARK: - picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\( myArr[row])"
}
}
我希望我帮助过你