简体   繁体   English

Swift 3 UIPickerView以编程方式开始旋转

[英]Swift 3 UIPickerView programmatically start to spin

I'm making some kind of wheel of fortune in iOS with Swift 3. I used a library with an UIPickerView, I customized the views, I made the rows endless (so the user has the idea that he scrolls through endless items). 我正在使用Swift 3在iOS中创造某种命运。我使用了带有UIPickerView的库,我自定义了视图,使行无休止(因此,用户认为他可以滚动查看无休止的项目)。

The only problem is that the user can swipe slow through the prices in the wheel of fortune, so basically he can pick whatever price he wants. 唯一的问题是用户可以通过命运之轮缓慢地滑动价格,因此基本上他可以选择自己想要的任何价格。

My though to preventing this was whenever the user taps or swipes the wheel, I make the UIPickerView spinning and with a random number I decide the outcome of the wheel of fortune. 尽管要防止这种情况发生,但是无论何时用户点击或滑动方向盘,我都会使UIPickerView旋转,并使用一个随机数来确定命运之轮的结果。

I know how to add gesture recognizers to the wheel, but the only thing I do not know is how to start the UIPickerView spinning programmatically. 我知道如何向方向盘添加手势识别器,但是我唯一不知道的是如何以编程方式启动UIPickerView。

Thanks in advance! 提前致谢!

I think you are looking for this: 我认为您正在寻找:

pick.selectRow(10, inComponent: 0, animated: true)

But if you want to see a full example I made this for you. 但是,如果您想查看完整的示例,我为您做了。

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])"
}


}

I hope I have helped you 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM