# UIGestureRecognizer Tutorial: Getting Started

**URL:** <https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704>\
**Category:** 🎓 Courses & Articles\
**Created:** [February 28, 2016, 8:01pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704 "2016-02-28T20:01:45Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [May 3, 2016, 11:43am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/21 "2016-05-03T11:43:31Z")

</div>

I’d advise not to use Auto Layout when you are changing the `frame` or `center` properties.

This works for me:

```auto
  @IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    guard let view = recognizer.view else { return }
    switch recognizer.state {
    case .Began:
      originalPosition = view.center
    case .Changed:
      let translation = recognizer.translationInView(self.view)
      view.center = CGPoint(x:view.center.x + translation.x,
                            y:view.center.y + translation.y)
      recognizer.setTranslation(.zero, inView: self.view)
    case .Ended:
      
      UIView.animateWithDuration(1,
                                 delay: 0,
                                 options: .CurveEaseOut,
                                 animations: {
                                  view.center = self.originalPosition
        },
                                 completion: nil)
    default: ()
    }
  }

```

---

<div class="post-metadata">

**Author:** ![myriam](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/myriam/32/31959_2.png) [@myriam](https://forums.kodeco.com/u/myriam)\
**Post date:** [May 3, 2016, 7:02pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/22 "2016-05-03T19:02:18Z")

</div>

Great, this works! Thank you! Can I set constraints programmatically, as I want to have the monkey in the upper left corner, or will there be problems too?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [May 4, 2016, 6:36am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/23 "2016-05-04T06:36:43Z")

</div>

If you’re only doing simple things, then you shouldn’t run into a problem.

You can always set constraints active or not active. You can also add your constraints into an array and set the entire array active or not.

Or instead of changing the center property, you could use the constraint’s constant property. (Although in a short test, I just added auto layout constraints in code, leaving the above method in place and it worked fine.)

Don’t forget to set the view’s `translatesAutoresizingMaskIntoConstraints` false if you are setting constraints in code.

---

<div class="post-metadata">

**Author:** ![myriam](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/myriam/32/31959_2.png) [@myriam](https://forums.kodeco.com/u/myriam)\
**Post date:** [May 4, 2016, 9:41pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/24 "2016-05-04T21:41:16Z")

</div>

Thank you! Changing the constraint’s constant worked in different scenarios and was petty straight forward. Mokeyproblem solved 🙂

---

<div class="post-metadata">

**Author:** ![slgraff](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/slgraff/32/111428_2.png) [@slgraff](https://forums.kodeco.com/u/slgraff)\
**Post date:** [May 18, 2016, 7:52pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/25 "2016-05-18T19:52:49Z")

</div>

I’m really enjoying this tutorial thus far. I’m using Xcode 7.3 with Swift 2.2 and am running into some problems getting the loadSound function to work. I changed it to use do/try/catch like this:

`func loadSound(filename:NSString) -> AVAudioPlayer { let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "caf") // Changed this from tutorial to work in Swift 2 do { let player = try AVAudioPlayer(contentsOfURL: url!) player.prepareToPlay() } catch { print("Error loading \(url)") } return player }`

The problem is that the line ‘return player’ gets the error “Use of unresolved identifier ‘player’” which I expect is because ‘player’ was created inside the do/try/catch statement. At this point I’m not sure how to resolve this.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [May 19, 2016, 8:52am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/26 "2016-05-19T08:52:57Z")

</div>

@sigraff - Yes, you’re right - by the time you get to `return player`, `player` is out of scope.

You need to define `player` before the `do`.

Just declare  
`let player: AVAudioPlayer`  
before the `do`, and then set `player` during the `do`.

---

<div class="post-metadata">

**Author:** ![zakia](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zakia/32/35205_2.png) [@zakia](https://forums.kodeco.com/u/zakia)\
**Post date:** [June 23, 2016, 2:58pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/27 "2016-06-23T14:58:42Z")

</div>

> [@caroline](#):
>
> [UIGestureRecognizer | Apple Developer Documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/)

Hi Caroline,  
Thank you so much for the tutorial, it really helped me to introduce to UIGestureRecognizer 🙂  
I’m trying to make a slight change but I’m not able to resolve it myself ☹ Maybe you could give me a hint 😉  
Problem: when I move the monkey or banana, it goes over all the buttons and NavigationBar… the pan gesture has no limits  
Solution I tried: I add the UIImageView (monkey/banana) to a UIView called “agregatedView”, where I thought the monkey would move only within the boundaries of this view.  
monkey.frame = CGRect(x: 0, y: 0, width: 100, height: 100)  
agregatedView.addSubview(monkey)  
monkey.userInteractionEnabled = true  
let recognizer = UIPanGestureRecognizer(target: self, action:Selector(“handlePan:”))  
monkey.addGestureRecognizer(recognizer)

But NO! the monkey is added to agregatedView, and I can move it wherever I want.  
Then, I tried to add the gesture to agregatedView itself, but of course, that moves the whole agregatedView.  
what should I do in this case? any help would be really appreciated 😊  
Thank you again!!

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [June 24, 2016, 5:37am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/28 "2016-06-24T05:37:18Z")

</div>

@zakia - the monkey won’t stay within the boundaries, because you can position subviews outside of the main view. For example, you may want to animate a view from off-screen to on-screen. In that case, you’d give the subview’s frame negative origin coordinates.

In your gesture handler, you can check the frame of the view that you are moving and check that it doesn’t go outside the view.

In the gesture handler, you have this code:

```auto
    if let view = recognizer.view {
      let translation = recognizer.translationInView(self.view)
      view.center = CGPoint(x:view.center.x + translation.x,
        y:view.center.y + translation.y)
      recognizer.setTranslation(CGPointZero, inView: self.view)
    ....

```

where you set the view’s center with the new position. If the view won’t end up in the area that you want to keep it in, you don’t have to set the new center.

---

<div class="post-metadata">

**Author:** ![timr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timr/32/35865_2.png) [@timr](https://forums.kodeco.com/u/timr)\
**Post date:** [June 30, 2016, 4:36am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/29 "2016-06-30T04:36:14Z")

</div>

Hi Caroline - great tutorial, thank you. In regards to the ‘Gratuitous Deceleration’ section, I wanted to allow the images to be stopped mid-slide by a tap gesture i.e. another pan gesture, but the gesture was only recognised after the image had stopped moving. I included `UIViewAnimationOptions.AllowUserInteraction` in the `animateWithDuration` options, which improved sensitivity, but only when the image was moving slowly and not quickly (very bizarre).

I was also struggling with getting the images to stop moving if they hit the edge of the screen during a slide. Any boundaries set such as:

```
UIView.animateWithDuration(Double(slideFactor * 2),
      delay: 0,
       // 6
      options: [UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.AllowUserInteraction],
      animations: { recognizer.view!.center = finalPoint
                    
          if self.Banana.frame.origin.x >= self.view.frame.width - self.Banana.frame.width {
               self.Banana.frame.origin.x = self.view.frame.width - self.Banana.frame.width  
          }
      },
      completion: nil)

```

… were affected by `finalPoint`, where the image slowed down so that it reached the boundary at the end of its slide instead of maintaining its speed and being stopped mid-slide.

I’m sure there are simple solutions, but perhaps you could advise me what to do here. Thanks once again

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [July 1, 2016, 12:34am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/30 "2016-07-01T00:34:40Z")

</div>

@timr - I think `UIViewAnimationOptions.AllowUserInteraction` _should_ work. But not 100% sure.

For the boundaries, you’ll need to take boundary testing out of `if recognizer.state == UIGestureRecognizerState.Ended`

Also `Banana` capitalized implies that it is a type (i.e. a class name). The terminology should be `self.bananaView.frame`

You could also experiment with using the touch methods such as `touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)`

---

<div class="post-metadata">

**Author:** ![timr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timr/32/35865_2.png) [@timr](https://forums.kodeco.com/u/timr)\
**Post date:** [July 1, 2016, 11:29pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/31 "2016-07-01T23:29:11Z")

</div>

For some reason .allowUserInteraction only works at the very end of animation. I’m absolutely baffled. I have tried using touchesMoved to intercept it the view, but I shouldn’t really need to. Besides, it just created new problems.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [July 1, 2016, 11:42pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/32 "2016-07-01T23:42:48Z")

</div>

Are you sure that you have .allowUserInteraction outside that checking state conditional? The original code only does everything when state is .Ended.

And have you tried it on a device?

---

<div class="post-metadata">

**Author:** ![timr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timr/32/35865_2.png) [@timr](https://forums.kodeco.com/u/timr)\
**Post date:** [July 2, 2016, 12:00am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/33 "2016-07-02T00:00:34Z")

</div>

I’ve tried putting `UIViewAnimationOptions.AllowUserInteraction` lines outside the .Ended if statement if that’s what you mean? But shouldn’t it work inside the statement since .allowUserInteraction is one of the animateWithDuration options? Doesn’t work on device either

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [July 5, 2016, 11:54pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/34 "2016-07-05T23:54:37Z")

</div>

@timr - I’ve thought about it at length. I think if you want to do this, don’t use gestures, but use touches began / moved / ended.

Here’s the thing:

The animation should be done, as it is in the tutorial, when the user lets go (state = .Ended).

At this point, the pan gesture is still being used, so .AllowUserInteraction is irrelevant.

To stop the animation, you need some kind of external stopping. For example, this stops the animation (note no fancy stuff, just stopping monkeyView from animating)

```auto
  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let presentationLayer = monkeyView.layer.presentationLayer() else { return }
    monkeyView.frame = presentationLayer.frame
    monkeyView.layer.removeAllAnimations()
  }

```

But the pan gesture is still in use, so you can’t just keep dragging the monkeyView without letting go the tap hold and starting the pan again.

I thought disabling the pan gesture and re-enabling the gesture might work, but of course if the tap is still down, then the pan gesture won’t fire.

That’s why I think using the touch methods rather than gestures would be more appropriate here.

But if you find a way and get it working with gestures, I’d love to know!

---

<div class="post-metadata">

**Author:** ![timr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timr/32/35865_2.png) [@timr](https://forums.kodeco.com/u/timr)\
**Post date:** [July 7, 2016, 3:05am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/35 "2016-07-07T03:05:19Z")

</div>

Yes it’s tricky isn’t it.

With what you’ve said and a bit of testing, I’ve decided it comes down to this:

1. If you use the pan with touchesBegan, touchesMoved, you can slide the view, but not in the same touch you stop the animation with.

2. If you use touchesBegan, touchesMoved, and touchesEnded alone, animations continue when any touch is released, but the slide animation doesn’t work.

However, [this post](http://stackoverflow.com/questions/27216528/how-to-drag-and-release-an-object-with-velocity) uses a different approach and finds a solution to this problem.

Thank you again for the great tutorial!

---

<div class="post-metadata">

**Author:** ![ian123](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ian123/32/63461_2.png) [@ian123](https://forums.kodeco.com/u/ian123)\
**Post date:** [July 28, 2016, 3:54pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/36 "2016-07-28T15:54:49Z")

</div>

`let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "caf")`

This line returns `nil`. Why might this be?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [July 29, 2016, 7:29am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/37 "2016-07-29T07:29:08Z")

</div>

I can’t see anything wrong with the line itself, so further investigation is necessary.

Xcode 7.3?  
What does filename contain?  
Do you have a filename.caf in your project?

---

<div class="post-metadata">

**Author:** ![aiyub](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aiyub/32/34070_2.png) [@aiyub](https://forums.kodeco.com/u/aiyub)\
**Post date:** [July 30, 2016, 7:22am UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/38 "2016-07-30T07:22:50Z")

</div>

Nice Code But Pan gesture is not working…

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [July 30, 2016, 12:53pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/39 "2016-07-30T12:53:21Z")

</div>

@aiyub - it should be working. Did you go through the tutorial? When did it stop working?

The sample code has a custom tickle gesture implemented, but you can uncomment a `return` line to implement the pan gesture instead of the tickle gesture.

---

<div class="post-metadata">

**Author:** ![alexbepple](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/alexbepple/32/38029_2.png) [@alexbepple](https://forums.kodeco.com/u/alexbepple)\
**Post date:** [July 31, 2016, 4:51pm UTC](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704/40 "2016-07-31T16:51:02Z")

</div>

In case anyone is interested: Xcode 7.3.1 code is at [GitHub - alexbepple/MonkeyPinch: Xcode 7.3.1 code for UIGestureRecognizer tutorial https://www.raywenderlich.com/76020](https://github.com/alexbepple/MonkeyPinch)

[Previous page](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704.md?page=1)

[Next page](https://forums.kodeco.com/t/uigesturerecognizer-tutorial-getting-started/704.md?page=3)
