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

时间:2019-07-19 22:21:33

标签: swift uibutton uipickerview uiactionsheet ios12

我创建了一个自定义操作表,其中包含一个UIPickerView和两个按钮(请参见下图)。

当我向下拖动滚动条时,我的手指(在屏幕截图上表示出来)在按钮“悬停”时按下了按钮。

最终我将手指放到取消按钮上,因此将触发取消事件(当然,确定按钮也是如此)。

要避免这种情况该怎么办?

enter image description here

我用于在应用中创建各种操作表的代码如下:

/**
 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)
}

1 个答案:

答案 0 :(得分:-1)

我可以看看在其中设置UIAlertController的代码吗?我试图复制您的UIAlertController,但没有得到您描述的问题。