//
Viewcontroller.m
05- gestures
//
Created by Wanghy on 15/9/21.
Copyright (c) year Wanghy. All rights reserved.
//
#import "ViewController.h"
@interface viewcontroller ()
@property (weak, nonatomic) iboutlet uiimageview* imageView;
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super viewdidload];
additional setup after loading the view, typically from a nib.
//1. create an object for a gesture
//2. add the object of the gesture to the view that requires gestures
//3. ways to implement gestures
//uitapgesturerecognizer ( percussion )-------------
//// 1. objects that create gestures
//uitapgesturerecognizer* tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)];
//// several fingers
//tap.numberoftouchesrequired = 2;
//// point a few times
//tap.numberoftapsrequired = 2;
//// 2. Adding gestures to ImageView
//[Self.imageview Addgesturerecognizer:tap];
//// 3. Implementation Method
//uiswipegesturerecognizer (swipe )-------------
//1.
Uiswipegesturerecognizer* swipe = [[uiswipegesturerecognizer alloc] initwithtarget :self action:@selector(swipe:)];
Uiswipegesturerecognizer* swipe1 = [[uiswipegesturerecognizer alloc] initwithtarget :self action:@selector(swipe:)];
// slide left
Swipe. direction = uiswipegesturerecognizerdirectionleft;
//2.
[self. ImageView addgesturerecognizer: swipe];
[self. ImageView addgesturerecognizer: swipe1];
//uilongpressgesturerecognizer ( Long press )-------------
//1.
Uilongpressgesturerecognizer* longpress = [[uilongpressgesturerecognizer alloc] Initwithtarget:self action:@selector(longpress:)];
// long-Press how long it takes to execute the method
Longpress. minimumpressduration = 2;
// error
Longpress. allowablemovement = ten;
//2.
[self. ImageView addgesturerecognizer: longpress];
//uirotationgesturerecognizer ( swivel )-------------
//1
Uirotationgesturerecognizer* rotation = [[uirotationgesturerecognizer alloc] Initwithtarget:self action:@selector(rotation:)];
//2.
[self. ImageView addgesturerecognizer: rotation];
//uipinchgesturerecognizer ( Pinch, for zooming )-------------
//1.
uipinchgesturerecognizer* pinch = [[uipinchgesturerecognizer alloc] initwithtarget :self action:@selector(pinch:)];
//2.
[self. ImageView Addgesturerecognizer:p inch];
//uipangesturerecognizer ( drag )-------------
//1.
uipangesturerecognizer* pan = [[uipangesturerecognizer alloc] initwithtarget: Self action:@selector(pan:)];
//2.
[self. ImageView Addgesturerecognizer:p an];
}
dragging
-(void) Pan: (uipangesturerecognizer*) sender
{
cgpoint p = [Sender translationinview:self. ImageView];
self. ImageView. Transform = cgaffinetransformtranslate(self. ImageView. Transform, p.x, p.y);
[Sender settranslation:cgpointzero inview:self. ImageView];
}
kneading
-(void) Pinch: (uipinchgesturerecognizer*) sender
{
//Self.imageView.transform = Cgaffinetransformmakescale (Sender.scale, sender.scale);
self. ImageView. Transform = Cgaffinetransformscale(self. ImageView. Transform, sender. Scale, sender. Scale);
Sender. scale = 1;
}
Rotate
-(void) Rotation: (uirotationgesturerecognizer*) sender
{
NSLog(@ "%f", sender. Rotation);
self. ImageView. Transform = cgaffinetransformrotate(self. ImageView. Transform, sender. Rotation);
Sender. rotation = 0;
//Self.imageView.transform = Cgaffinetransformmakerotation (sender.rotation);
}
Long Press
-(void) longpress: (uilongpressgesturerecognizer*) sender
{
// just want to start by executing a code that needs to determine the state of the gesture
if (sender. state = = Uigesturerecognizerstatebegan) {
NSLog(@ "longpress");
}
}
Swipe
-(void) Swipe: (uiswipegesturerecognizer*) sender
{
if (sender. Direction = = Uiswipegesturerecognizerdirectionleft) {
NSLog(@ "left");
}
else {
NSLog(@ "right");
}
}
percussion
-(void) Tap: (uitapgesturerecognizer*) sender
{
NSLog(@ "tap");
}
@end
Several common gestures for uiimageview in iOS development