2016-10-06 85 views
0

我使用Swift 3在iOS中創造了一些幸運之輪。我使用了一個帶有UIPickerView的庫,我自定義了這些視圖,我做了無盡的行(所以用戶擁有想法,他滾動通過無盡的項目)。Swift 3 UIPickerView以編程方式開始旋轉

唯一的問題是,用戶可以通過財富輪中的價格輕掃,所以基本上他可以選擇他想要的任何價格。

我雖然防止這種情況是每當用戶輕敲或滑動車輪,我使UIPickerView旋轉,隨機數字我決定命運之輪的結果。

我知道如何在輪子上添加手勢識別器,但唯一不知道的是如何以編程方式啓動UIPickerView旋轉。

在此先感謝!

回答

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


} 

我希望我已經幫你

+0

正是我想要的!現在,我只需要找到一種方法來禁用緩慢滑動的輪子,或者只是通過點擊即可禁用滑動!布謝謝你,這正是我正在尋找的東西! – Charlotte1993

相關問題