Simple Gesture operations in iOS: Long-pressed, kneading, moving, and rotating
Create a single view project
ViewController. h file
#import
@interface ViewController : UIViewController { UIImageView *_imgView; float _rotation;}@end
ViewController. m file
# Import "ViewController. h "@ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; _ imgView = [[UIImageView alloc] initWithFrame: CGRectMake (110,160,100,150)]; _ imgView. image = [UIImage imageNamed: @ "10_0.jpg"]; [self. view addSubview: _ imgView]; [_ imgView release]; _ imgView. userInteractionEnabled = YES; // long press UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPress :)]; // longPress. minimumPressDuration = 0; // The effect is equal to that of longPress. minimumPressDuration = 2; // The event is triggered in at least two seconds. // [_ imgView addGestureRecognizer: longPress]; [longPress release]; // kneading keys * pinch = [[using alloc] initWithTarget: self action: @ selector (pinchPress :)]; [_ imgView addGestureRecognizer: pinch]; [pinch release]; // move the cursor * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (pan :)]; [_ imgView addGestureRecognizer: pan]; [pan release]; // rotate * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (rotation :)]; [_ imgView addGestureRecognizer: rotation]; [rotation release]}; -(void) rotation :( UIRotationGestureRecognizer *) rotation {_ imgView. transform = CGAffineTransformMakeRotation (_ rotation + rotation. rotation); if (rotation. state = UIGestureRecognizerStateCancelled) {_ rotation = rotation. rotation + _ rotation;} NSLog (@ "% f", _ rotation);}-(void) pan :( UIPanGestureRecognizer *) pan {CGPoint point = [pan translationInView: self. view]; _ imgView. center = CGPointMake (_ imgView. center. x + point. x, _ imgView. center. y + point. y); [pan setTranslation: CGPointZero inView: self. view]; // reset reference position}-(void) pinchPress :( UIPinchGestureRecognizer *) pinch {CGSize _ imgSize = CGSizeMake (100,150); float scale = pinch. scale; _ imgView. bounds = CGRectMake (0, 0, _ imgSize. width * scale, _ imgSize. height * scale); // determines the gesture status if (pinch. state = UIGestureRecognizerStateCancelled) {_ imgSize = _ imgView. bounds. size;} if (_ imgSize. height <= 75) {_ imgSize. height = 75; _ imgSize. width = 50 ;}- (void) longPress :( UILongPressGestureRecognizer *) lp {NSLog (@ "Long press");} @ end