Post not yet marked as solved
Hi,At the end I used Gesture from story board and I make 2 @IBAction (for each gesture) and it worksand it much easier and less coding.
Post not yet marked as solved
Hi,Here my entire codeimport UIKit
class ThirdViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let countrylists = [
"",
"Dubai",
"Geneva",
"Singapore"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countrylists.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return countrylists[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selection = countrylists[row]
switch selection {
case "Dubai":
lbl2.text = "999"
lbl3.text = "998"
lbl4.text = "997"
case "Geneva":
lbl2.text = "117"
lbl3.text = "118"
lbl4.text = "144"
case "Singapore":
lbl2.text = "999"
lbl3.text = "998"
lbl4.text = "997"
default:
lbl2.text = selection
}
}
@IBOutlet weak var lbl2: UILabel!
@IBOutlet weak var lbl3: UILabel!
@IBOutlet weak var lbl4: UILabel!
@IBOutlet weak var pvemergency: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pvemergency.dataSource = self
pvemergency.delegate = self
lbl2.isUserInteractionEnabled = true
lbl3.isUserInteractionEnabled = true
lbl4.isUserInteractionEnabled = true
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress))
view.addGestureRecognizer(longPressGesture)
//lbl3.addGestureRecognizer(longPressGesture)
//lbl4.addGestureRecognizer(longPressGesture)
longPressGesture.minimumPressDuration = 2.0
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap))
view.addGestureRecognizer(doubleTapGesture)
//lbl3.addGestureRecognizer(doubleTapGesture)
//lbl4.addGestureRecognizer(doubleTapGesture)
doubleTapGesture.numberOfTapsRequired = 2
}
@objc func longPress(){
//let phone = String(lbl2.text!)
//callNumber(phonenumber: phone)
UIPasteboard.general.string = lbl2.text //here i will need to detect which label i press
createAlert(title: "Infos", message: "Copied")
}
@objc func doubleTap (){
lbl2.text = "double tap" //same here, need to detect which label i double tap
let phone = String(lbl2.text!)
callNumber(phonenumber: phone)
//UIPasteboard.general.string = lbl2.text
}
func callNumber (phonenumber:String)
{
if let phoneCallURL = URL(string: "tel://\(phonenumber)"){
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)){
application.open(phoneCallURL,options: [:], completionHandler: nil)
}
}
}
func createAlert(title:String, message:String){
let msgalert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert )
msgalert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in
msgalert.dismiss(animated: true, completion: nil)
}))
self.present(msgalert, animated: true, completion: nil)
}Thanks
Post not yet marked as solved
you mean if i do a print(theLabel) ?If that, I got "Nothing"
Post not yet marked as solved
Thanks for the reply.Noted to insert code with <>if i tried your code i got :Could not cast value of type 'UIView' (0x1f20f9408) to 'UILabel' (0x1f20f8c10)I assume this line :let theLabel = gestureRecognizer.view as! UILabel detect which label I double tap?Thanks