Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gestures UIGestureRecognizer.

Similar presentations


Presentation on theme: "Gestures UIGestureRecognizer."— Presentation transcript:

1 Gestures UIGestureRecognizer

2 gestures There are 6 default gestures recognized by iOS:
Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer) Pinch gesture (UIPinchGestureRecognizer) Rotate gesture (UIRotationGestureRecognizer) Swipe gesture (UISwipeGestureRecognizer) Long press gesture (UILongPressGestureRecognizer) You can also make your own gesture recognizers Reference tutorial: uigesturerecognizer-with-swift-tutorial

3 Process Create an instance of a gesture recognizer
Attach the instance to a view Create the gesture handler Can be done programmatically or via IB we’ll do it programmatically

4 Example Create a new project (can be a single view or a tabbed view)
Put a label on the view and connect an IBOutlet to the label Add an imageView from the library. Add an image to your project and assign it to the image view. Add an IBOutlet and connect it to your image. Name the outlet theImage Make sure that User Interaction Enabled is checked in both the identity inspector and the attributes inspector

5 Pan Gestures This allows you to move a view.
In the storyboard, drag a “Pan Gesture Recognizer” from the object library onto the view. Click on the image and then look in the Connections Inspector. Note that the outlet collections now has a connection to the gesture recognizer In the Connections Inspector, drag from the “Pan Gesture Recognizer” in the Document Outline to the “View Controler”

6 Pan Gestures Add to the ViewController.swift file:
translation gives the amount the view has moved relative to the starting point Add to the ViewController.swift file: @IBAction func handlePan(recognizer:UIPanGestureRecognizer) { let translation = recognizer.translationInView(self.view) if let view = recognizer.view { view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y) } recognizer.setTranslation(CGPointZero, inView: self.view) Store the new view center in view.center Update the view center to the new center

7 Run You should be able to move the image.

8 Tap gesture We will do this one programmatically.
Add the variable numTaps and then change viewDidLoad: var numTaps: Int = 0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:")) recognizer.delegate = self theImage.addGestureRecognizer(recognizer) } numTaps keeps track of the number of times the image has been touched recognizer is a constant that refers to the TapGesture We make the tapGesture delegate this viewController Must add the TapGesture recognizer to the image. Note that theImage is the name of our IBOutlet for the image in the storyboard.

9 ViewController.swift Add this method to ViewController.swift:
func handleTap(recognizer: UITapGestureRecognizer) { numTaps++ tapsField.text = String(numTaps) } numTaps keeps track of the number of times the image has been touched Change the label to include the number of taps

10 run If tapping on the image doesn’t change the label, make sure that you’ve clicked the “User Interaction Enabled” checkbox on the image in the storyboard.

11 Pinch gesture Drag a “Pinch Gesture Recognizer” from the Object Library to the image. Connect the gesture recognizer to the view controller as you did for the pan gesture. Copy the code below to your ViewController.swift @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) { if let view = recognizer.view { view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale) recognizer.scale = 1 }

12 run Should be able to expand/shrink the image


Download ppt "Gestures UIGestureRecognizer."

Similar presentations


Ads by Google