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

标签 swift uigesturerecognizer uipickerview spinning

我正在使用 Swift 3 在 iOS 中做一些幸运之轮。我使用了一个带有 UIPickerView 的库,我自定义了 View ,我让行变得无穷无尽(所以用户有他滚动浏览无穷无尽项目的想法).

唯一的问题是用户可以在命运轮盘中缓慢滑动价格,所以基本上他可以选择他想要的任何价格。

我的想法是,每当用户点击或滑动转盘时,我都会让 UIPickerView 旋转,并使用随机数来决定命运转盘的结果。

我知道如何向滚轮添加手势识别器,但我唯一不知道的是如何以编程方式启动 UIPickerView 旋转。

提前致谢!

最佳答案

我想你正在寻找这个:

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


}

希望对你有帮助

关于Swift 3 UIPickerView 以编程方式开始旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896696/

相关文章:

swift - UIPickerView抖动

swift - 有什么方法可以获取 firebase 请求的状态?

swift - NSOutlineView 绑定(bind)到 NSTreeController (节点在窗口调整大小时折叠)

ios - 在 1place 的完整项目中快速设置不同样式的字体

ios - UITableViewCell 上的 UIPanGestureRecognizer 覆盖了 UITableView 的 ScrollView 手势识别器

ios - 自定义 UIPickerView

ios - iOS Cocoa-自定义uipickerview或类似版本

ios - 如何从数组中只传递一个键值对?

iOS 谷歌地图添加额外的 UIGesturerecognizer

swift - Pan 手势和 Tap 手势之间的冲突