October 2013
Intermediate to advanced
1053 pages
28h 7m
English
You want your users to be able to perform pinch gesture on a view.
Create an instance of the UIPinchGestureRecognizer class and add it to
your target view, using the addGestureRecognizer: instance method of the
UIView class:
-(void)viewDidLoad{[superviewDidLoad];CGRectlabelRect=CGRectMake(0.0f,/* X */0.0f,/* Y */200.0f,/* Width */200.0f);/* Height */self.myBlackLabel=[[UILabelalloc]initWithFrame:labelRect];self.myBlackLabel.center=self.view.center;self.myBlackLabel.backgroundColor=[UIColorblackColor];/* Without this line, our pinch gesture recognizerwill not work */self.myBlackLabel.userInteractionEnabled=YES;[self.viewaddSubview:self.myBlackLabel];/* Create the Pinch Gesture Recognizer */self.pinchGestureRecognizer=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePinches:)];/* Add this gesture recognizer to our view */[self.myBlackLabeladdGestureRecognizer:self.pinchGestureRecognizer];}
The view controller is defined in this way:
#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)UIPinchGestureRecognizer*pinchGestureRecognizer;@property(nonatomic,strong)UILabel*myBlackLabel;@property(nonatomic,unsafe_unretained)CGFloatcurrentScale;@end
Pinching allows users to scale GUI elements up and down easily. For instance, the Safari web browser on iOS allows users to pinch on a web page in order to zoom into the contents ...
Read now
Unlock full access