Pinch Gesture not working

I am playing around with Pinch Gesture for fun to try to learn, however, I do not see my "PINCH" print statement displayed in the console.


Question

Why does my print("PINCH") statement not show up in the Console when the code is run?

(Note: After the program is running in the simulator, I press the <Option> key and see the two "pinch" circles. I then use the mouse to move the circles closer together and then farther apart)


Initial Setup

- Added a new ViewController in my storyboard

- Created a New Cocoa Touch File (type of ViewController) and called it "GesturesViewController"

- Associated the "GesturesViewController" class with the ViewController object I added previously so they are linked

- Dragged a UIView object on the ViewController and resized it to be large

- Created an IBOutlet of the UIView and called it "letterView" (see code added below)

- Defined the pinch gesture recognizer within the didSet{ } of the IBOutlet letterView to call "processPinch" method (see code below)

- Run the code, do the "pinching" with the two circles and the mouse and the "PINCH" text does not show up in the Console


Code:



import UIKit
class GesturesViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    @IBOutlet weak var letterView: UIView! {
      didSet
      {
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(processPinch(_:)))
        letterView.addGestureRecognizer(pinch)
      }
    }

    @objc func processPinch(_ sender: UIPinchGestureRecognizer)
    {
        print("PINCH")
    }
}

Accepted Reply

RESOLVED :>)


This was definitely "user error" as expected, but I just could not figure it out until now.

I was doing the "pinch" with the mouse incorrectly.


This is what I was doing when it DID NOT work:


- Press the "Option" key

- See the two circles

- Move the mouse back and forth


This is what I was doing when it DID work:


- Press the "Option" key

- See the two circles

- Click the mouse button (to simulate touching the screen)

- Move the mouse back and forth


Thanks for all the support . I am very glad I figured this one out.

Replies

I modified the auto generated ViewController.swift of Single View App template project, as to the code you have shown and I could find PINCH in my debug console each time I pinched.


So, it's not the problem of code, but any of your settings may be wrong.


What happens when you put a breakpoint on line 12 and re-run?

Thanks for the reply ...


Just to be clear ... I originally created a new project which provides a default "ViewController". I then added a new/second ViewController to my storyboard and named it GesturesViewController. There is a button on the first/default ViewController which, when pressed, will seque to my GesturesViewController. I added a UIView (ie: letterView) to the GesturesViewController. This GesturesViewController class is the code I am sharing with you below.


I also confirmed the isUserInteractionEnabled property is properly set to true (ie: checkmarked in the Identiy Inspector) for my letterView


I added print statements on line 12, 14 and 16 (see below) and confirmed I see all of them displayed in the Console, as expected, when the GestureViewController is first loaded. When I press the <Option> key I see the two circles, but still "PINCH" is not displayed when I moved the circles together or apart via the mouse :>(


Is there a way I could check the return value from the call in Line 15?


import UIKit
class GesturesViewController: UIViewController {

   override func viewDidLoad()
   {
       super.viewDidLoad()
   }

   @IBOutlet weak var letterView: UIView! {
    didSet
    {
print("1111")
      let pinch = UIPinchGestureRecognizer(target: self, action: #selector(processPinch(_:)))
print("2222")
      letterView.addGestureRecognizer(pinch)
print("3333")
    }
  }

   @objc func processPinch(_ sender: UIPinchGestureRecognizer)
   {
       print("PINCH")
   }
}

I added print statements on line 12, 14 and 16 (see below) and confirmed I see all of them displayed in the Console


Thanks for clarifying, that info filters out many possibilities.


I tried your code for an added view controller, and all the same. Your code works perfectly, and I found PINCHes in my debug console.


There may be something wrong where you have not shown yet. Or you may not be showing the exact code causing the issue.

> Dragged a UIView object on the ViewController and resized it to be large


Did you add any layout constraints? What is its frame at runtime? (use view debugger or print out in viewDidLayoutSubviews)

I am using "Xcode 11.0 (11A420a)"


I decided to create a New Project (from scratch) and same issue exists.


Steps performed:


- Create a new project in Xcode ("Single View App" with Language=Swift, User Interface=Storyboard)

- Drag a UIView (ie: View Object) into the Storyboard and resize it to cover the full view (no constraints applied)

- Copy the code below into the "ViewController.swift" file

- Click on UIView, press "Ctrl" then drag from UIView to the IBOutlet defined in the code (ie: letterView) to name the UIView

- Build/Run the project

- Press <Option> key and see the two circles

- Move mouse around so circles come together and go apart

- I do not see "PINCH" text on the Console output


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    @IBOutlet weak var letterView: UIView!
    {
      didSet
      {
  print("1111")
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(processPinch(_:)))
  print("2222")
        letterView.addGestureRecognizer(pinch)
  print("3333")
      }
    }

    @objc func processPinch(_ sender: UIPinchGestureRecognizer)
    {
        print("PINCH")
    }
}

no constraints applied


Without constraints, you cannot expect where the view would be placed.

Are you sure the added UIView is displayed in the window? Have you set the background color of it?

This is not a project with a ton of objects in it :>) This is a project which I created to teach myself pinch gesture only. I created a brand new project and dragged "one" UIView object (only object used) right into the center of the default ViewController so I know exactly where it is located.


I decided to color the background of the ViewController to orange just now, and I can confirm the UIView is right in the center when the project executes, as expected.


I also decided to add constaints just for fun (ie: Top Safe Area, Bottom Safe Area, Leading Safe Area and Trailing Safe Area)


However, it still does not print "PINCH" text for me :>(


Are you using "Xcode 11.0 (11A420a)" like I am ?

I decided to color the background of the ViewController to orange just now, and I can confirm the UIView is right in the center when the project executes


Thanks for clarifying. I use the exact same version of Xcode and followed the steps except the following:

- (no constraints applied) → I added constraints (and colored it orange)

- Click on UIView, press "Ctrl" then drag from UIView to the IBOutlet defined in the code (ie: letterView) to name the UIView

→ At this time, ViewController.swift had alread `letterrView` as IBOutlet, so I clicked on the white circle of it and ctrl-dragged to the UIView.


And thease are the outputs (copied without modifying), shown in the debug console:

1111

2222

3333

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH

PINCH


Seems we need to find other possibilites where is wrong, but as for now, I cannot think of any.

To reduce the code to a minimum, I added a "PInch Gesture Recognizer" object from the Object Library into a new project,

but still it does not work :>(


PS: I am not using an Apple mouse ... would this matter at all?



Steps performed:

- Create a new project in Xcode ("Single View App" with Language=Swift, User Interface=Storyboard)

- Drag a UIView (ie: View Object) into the Storyboard and resize it to cover the full view

- Drag a "PInch Gesture Recognizer" object from the Object Library into my UIView object

- Select the little grey box on top of the View Controller in the storyboard (ie: pinch gesture grey box)

- Press "Ctrl" on keyboard and drag a line from the little grey box to the ViewController code to create a @IBAction called pinchIt


import UIKit
class ViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    @IBAction func pinchIt(_ sender: Any) {
        print("PINCH IT")
    }
}


- Build/Run the project

- Press <Option> key and see the two circles

- Move mouse around so circles come together and go apart

- I do not see "PINCH IT" text on the Console output

PS: I am not using an Apple mouse ... would this matter at all?

I am not neither, and very unlikely that would affecct.


Steps followed:

- Create a new project in Xcode ("Single View App" with Language=Swift, User Interface=Storyboard)

→I named the project PinchIt. But I believe the name of the project does not affect.

- Drag a UIView (ie: View Object) into the Storyboard and resize it to cover the full view

→Dragged to the View Controller Scene on the Main.storyboar, added constraints using Add Missing Constraints and colored it orange.

- Drag a "PInch Gesture Recognizer" object from the Object Library into my UIView object

- Select the little grey box on top of the View Controller in the storyboard (ie: pinch gesture grey box)

- Press "Ctrl" on keyboard and drag a line from the little grey box to the ViewController code to create a @IBAction called pinchIt

→Needed to open the Assistent Editor before doing this

- Build/Run the project

→Added a line `print("PINCH IT")` before running it

- Press <Option> key and see the two circles

→Before doing this, I confirmed that the orange area displayed in the sim covering the window

- Move mouse around so circles come together and go apart

→Kept pressing Option while moving the mouse cursor

- I do not see "PINCH IT" text on the Console output

→Plenty of PINCH IT shown on the debug console

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

PINCH IT

↑Copied from the actual output.

I do not understand why it does not work for me. The code is so basic.

Anyway, I do not know anything else to try, so I guess I will not use pinch gestures in my apps


Oh well ... thanks for spending time testing

Sorry I cannot be any help.


Have you ever used sort of pods or some other package tools? Some of them break Xcode permanently...

Or it may be a problem of your Mac...


Anyway, hope you can solve your issue soon and your app be great.

RESOLVED :>)


This was definitely "user error" as expected, but I just could not figure it out until now.

I was doing the "pinch" with the mouse incorrectly.


This is what I was doing when it DID NOT work:


- Press the "Option" key

- See the two circles

- Move the mouse back and forth


This is what I was doing when it DID work:


- Press the "Option" key

- See the two circles

- Click the mouse button (to simulate touching the screen)

- Move the mouse back and forth


Thanks for all the support . I am very glad I figured this one out.

If you want to simulate pinh on simulator, it is really better to have a trackpad.

Thanks for reporting. I am verrrrry glad to hear you solved it.


I should have noticed it sooner but it was hard to me to find the slight (buf significant) difference...

Your experience is so suggestive and I will always have it in mind. Thanks.