Search code examples
swiftuigesturerecognizeruipickerviewspinning

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).

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.

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.

Thanks in advance!


Solution

  • 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