Quantcast
Channel: Active questions tagged uipickerview - Stack Overflow
Viewing all articles
Browse latest Browse all 592

How to prevent buttons to be triggered while dragging the UIPickerView?

$
0
0

I created a custom action sheet that contains one UIPickerView and two buttons (see the image below).

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?

enter image description here

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

Viewing all articles
Browse latest Browse all 592

Trending Articles