Home > Mobile >  Tap gesture in a view controller causing delay in opening of the view controller when a button in ot
Tap gesture in a view controller causing delay in opening of the view controller when a button in ot

Time:10-11

I have implemented a tap gesture on UIStackView in one ViewController which is embedded in a Tab Bar. When I click button on tab bar to open it, it causes a delay of few seconds and then opens the View Controller. After searching for a while on internet I found out tap gestures might cause this and apparently when I remove this tap gesture, it works smoothly. The solution they suggested was just remove tap gesture as in most cases they did not want it and it was accidentally there however I do want tap gestures so I cannot remove it. This is how I have added a simple tap gesture in viewDidLoad():

        let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
        tapAddImage.cancelsTouchesInView = false
        svAddImage.addGestureRecognizer(tapAddImage)

This function opens image picker.

Also I just noticed the same issue is caused in other ViewControllers too where I have implemented imagePicker which is opened by tapping something using Tap Gesture. Here is my image picker delegate functions:

 @objc func addImage(_ sender: UITapGestureRecognizer) {
        openCameraOrGallery()
    }

   func openCameraOrGallery() {
        let alert = UIAlertController(title: BaseUrl.shared.projectName, message: "Select Option", preferredStyle: .actionSheet)
               
        alert.addAction(UIAlertAction(title: "Camera", style: .default , handler:{ (UIAlertAction)in
            self.present(self.imagePicker!, animated: true, completion: {
                self.imagePicker?.sourceType = .camera
                self.imagePicker?.delegate = self
            })
        }))
               
        alert.addAction(UIAlertAction(title: "Gallery", style: .default , handler:{ (UIAlertAction)in
            self.present(self.imagePicker!, animated: true, completion: {
                self.imagePicker?.sourceType = .photoLibrary
                self.imagePicker?.delegate = self
            })
        }))
               
        alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
            print("User click Dismiss button")
        }))
        
        self.present(alert, animated: true, completion: {
            print("completion block")
        })
    }
    
    //MARK: UIImagePickerControllerDelegate
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        guard let tempImage:UIImage = info [.originalImage] as? UIImage else {return}
        
        self.ivSelectedPostImage.image = tempImage
        
        let imageData:NSData = tempImage.jpegData(compressionQuality: 0.2)! as NSData
        postBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
        
        self.svAddPostContentButtons.isHidden = true
        self.viewSelectedPostImage.isHidden = false
        
        self.dismiss(animated: true, completion: nil)
    }
    
    func cancelButtonDidPress(_ imagePicker: UIImagePickerController) {
        imagePicker.dismiss(animated: true, completion: nil)
    }

I have just copy pasted this code from my other project. There is no issue in any other project with ImagePicker Delegates or tap gestures. Any idea what might be causing delay only in these pages where I open camera or gallery by using tap gestures? This is only happening to those ViewControllers where im clicking a button to open these ViewControllers having tap gestures.

CodePudding user response:

Please try to initialise the tap gesture in ViewDidAppear.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
    tapAddImage.cancelsTouchesInView = false
    svAddImage.addGestureRecognizer(tapAddImage)

}
  • Related