Two finger swipe on a UIImage

The code below allows the user to do a 2 finger swipe down on an imageView and thus presenting a popover/actionSheet. That process works fine. Normally it is possible to tap outside the popover/actionSheet to close it.


The problem is that once the popover/actionSheet is presented, it doesn't allow tapping the background to close the popover/actionSheet. You actually need to tap inside the popover/actionSheet to close it.


There are other places in the app that present a popover/actionSheet but these are presented using a simple button tap.


Here's the really weird scenario. If I do the 2 finger swipe on the imageView and open the popover/actionSheet, the inability to tap the backGround is broken on all the other popover/actionSheet in the app too. If I bypass the 2 finger swipe on the imageView all of the other popover/actionSheet work as normal.


I've stripped out all the code other than what's needed to present the popover/actionSheet. And I created a new project with on VC and one imageView so as to eliminate any possible conflict with cocoa pod, etc. What is wrong with this code?


class ViewController: UIViewController
{
    @IBOutlet weak var imageView_Outlet: UIImageView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        imageView_Outlet.isUserInteractionEnabled = true

        let swipeGuesture = UISwipeGestureRecognizer(target: self, action: #selector(imageViewSwiped(recognizer:)))
        swipeGuesture.numberOfTouchesRequired = 2
        swipeGuesture.direction = .down
        imageView_Outlet.addGestureRecognizer(swipeGuesture)
    }

    @objc func imageViewSwiped(recognizer: UISwipeGestureRecognizer)
    {
        let theAlert = UIAlertController(title: "Welcome Image", message: "Only one image can be saved as your welcome screen. The current image will automatically be replaced." , preferredStyle: .actionSheet)
        let chooseImage = UIAlertAction(title: "Choose a New Image", style: .default, handler: { (okAction) in
        })
        let deleteBtn = UIAlertAction(title: "Delete the Current Image", style: .destructive, handler: { (deleteAction) in
        })

        let cancelBtn = UIAlertAction(title: "Cancel", style: .cancel) { (cancelAction) in
        }

        theAlert.addAction(cancelBtn)
        theAlert.addAction(chooseImage)
        theAlert.addAction(deleteBtn)

        let popOver = theAlert.popoverPresentationController
        popOver?.sourceView = self.imageView_Outlet
        popOver?.sourceRect = self.imageView_Outlet.bounds
        popOver?.permittedArrowDirections = .any
        present(theAlert, animated: true)
    }
}
Answered by Claude31 in 420592022

Yes, real test should be done on device. Curiously, I read nowhere this issue with simulator.


I tested the Swipe on an iPad (for double check) and itworks OK.


Good luck with your publishing.


Please don't forget to close the thread.

I tried your code, in iPhone 11 simulator.


Even with numberOfTouches = 1:

- I get the alert

- but cannot dismiss it unless I click one of the buttons.


Do you test on iPad ?

I can get it on iPad and effectively it works with touches = 1.


But I cannot get the swipe call the alert with touches = 2.

In fact, pointer does not move at all when I swipe, anywhere on the image or even superview.


UPDATE:

Tested on device (iPhone XS, iOS 13.4.1).

Swipe with 2 fingers work.

I can close the alert with a tap anywhere outside alert (in or out image).


So, could you explain the problem a bit more:

Here's the really weird scenario. If I do the 2 finger swipe on the imageView and open the popover/actionSheet, the inability to tap the backGround is broken on all the other popover/actionSheet in the app too.


- What do you expect when you tap background ?

=> I tapped on an notehr image and got the log I was expecting as well as the alert closing.

Hi Claude,

Thanks you for checking into this.


I also tested the app on my iPhone XS MAX and my iPhone 6s and you are correct. It does work as expected on the physical devices. I apologize for not following through on testing on the device. I guess I just got wrapped around the axle when it didn't work on the sim.


I'm in the final stages of finishing a series of 8 apps and this through a monkey wrench into the works.


I do not have an iPad to test on. I'll try to find someone with an iPad to test it. Until then I'm not comfortable releasing any apps using this code.


Thank you,

Tom

Accepted Answer

Yes, real test should be done on device. Curiously, I read nowhere this issue with simulator.


I tested the Swipe on an iPad (for double check) and itworks OK.


Good luck with your publishing.


Please don't forget to close the thread.

Two finger swipe on a UIImage
 
 
Q