gestures uigesturerecognizer. gestures there are 6 default gestures recognized by ios: tap gesture...
TRANSCRIPT
![Page 1: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/1.jpg)
GesturesUIGestureRecognizer
![Page 2: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/2.jpg)
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: http://www.raywenderlich.com/76020/using-uigesturerecognizer-with-swift-tutorial
![Page 3: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/3.jpg)
Process
Create an instance of a gesture recognizer
Attach the instance to a view
Create the gesture handler
Can be done programmatically or via IBwe’ll do it programmatically
![Page 4: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/4.jpg)
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
![Page 5: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/5.jpg)
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”
![Page 6: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/6.jpg)
Pan Gestures
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)
}
Update the view center to the new center
translation gives the amount the view has moved relative to the starting point
Store the new view center in view.center
![Page 7: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/7.jpg)
Run
You should be able to move the image.
![Page 8: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/8.jpg)
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 TapGestureWe 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.
![Page 9: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/9.jpg)
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
![Page 10: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/10.jpg)
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.
![Page 11: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/11.jpg)
Pinch gestureDrag 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
}
}
![Page 12: Gestures UIGestureRecognizer. gestures There are 6 default gestures recognized by iOS: Tap gesture (UITapGestureRecognizer) Pan gesture (UIPanGestureRecognizer)](https://reader035.vdocuments.mx/reader035/viewer/2022062721/56649f1e5503460f94c367a9/html5/thumbnails/12.jpg)
run
Should be able to expand/shrink the image