gestures uigesturerecognizer. gestures there are 6 default gestures recognized by ios: tap gesture...
Post on 04-Jan-2016
240 Views
Preview:
TRANSCRIPT
GesturesUIGestureRecognizer
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
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
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
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”
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
Run
You should be able to move the image.
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.
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
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.
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
}
}
run
Should be able to expand/shrink the image
top related