简体   繁体   English

如何防止在拖动UIPickerView时触发按钮?

[英]How to prevent buttons to be triggered while dragging the UIPickerView?

I created a custom action sheet that contains one UIPickerView and two buttons (see the image below). 我创建了一个包含一个UIPickerView和两个按钮的自定义操作表(请参见下图)。

As I drag the roll downwards, my finger (I represented it over the screenshot) presses the button when it “hovers” it. 当我向下拖动滚动条时,我的手指(我在屏幕截图上表示出来)在按钮“悬停”时按下了按钮。

Eventually I release my finger over the cancel button therefore the cancel event will be fired (of course, same applies to the Ok button). 最终,我将手指松开在“取消”按钮上,因此将触发“取消”事件(当然,同样适用于“确定”按钮)。

What should I do to this for not to occure? 我应该怎么做才不会发生?

在此处输入图片说明

My code to create various action sheets in my app is as follows: 在应用程序中创建各种操作表的代码如下:

/**
 Creates and present action sheet
 */
public func createAndPresentActionSheet(_ parent: UIViewController, title: String, message: String, tag: PickerDataSource, _ completion: @escaping ((_ action: UIAlertAction)->())) {

    let vc = UIViewController()
    vc.preferredContentSize = CGSize(width: 250,height: 200)

    let pickerView = UIPickerView()
    pickerView.isExclusiveTouch = true
    pickerView.tag = tag.rawValue
    pickerView.delegate   = parent as? UIPickerViewDelegate
    pickerView.dataSource = parent as? UIPickerViewDataSource
    vc.view.addSubview(pickerView)
    pickerView.stitchWithConstraints(to: vc.view)

    var row = 0
    switch tag {
    case .gas:                      row = -1 + GasStation.carGas.rawValue

    case .averageSpeed:             row = -1 + Int(GasStation.carAverageSpeed*1e-3)
    case .consumptionPer100km:      row = -1 + Int(GasStation.carGasConsumptionPer100km)
    case .tankVolume:               row = -1 + Int(GasStation.carGasTankVolume)
    case .usualVolumeRefill:        row = -1 + Int(GasStation.carGasUsualRefill)

    case .databaseMinimumFreshness: row = 0
    }

    print("\(#function): row = \(row)")

    if row < 0 { row = 0 }

    print("\(#function): row = \(row) once fixed from being negative")

    pickerView.selectRow(row, inComponent: 0, animated: true)



    let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
    alert.setValue(vc, forKey: "contentViewController")
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in

        if let tvc = parent as? UITableViewController {
            // Reload from data source
            tvc.tableView.reloadData()

            // Force display
            //tvc.tableView.setNeedsDisplay()
        }
        // Call completion
        return completion(action)
    }))
    alert.addAction(UIAlertAction(title: "Annuler", style: .cancel, handler: nil))
    parent.present(alert, animated: true)
}

May I look at the code where you set up the UIAlertController? 我可以看看在其中设置UIAlertController的代码吗? I tried to duplicate your UIAlertController and didn't get the problem you described. 我试图复制您的UIAlertController,但没有得到您描述的问题。

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

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