有没有办法让UIPickerView像彩票一样“旋转”。我想做一个UIPickerView和一个按钮。当用户按下按钮时,我希望UIPickerView旋转一段时间,然后在任意位置停止。这有可能吗?
发布于 2018-03-29 21:25:26
我认为你正在寻找这个:
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])"
}
}我希望这对你有帮助
https://stackoverflow.com/questions/49557191
复制相似问题