iOS常用手势识别器

手势识别状态:

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

// 没有触摸事件发生,所有手势识别的默认状态

UIGestureRecognizerStatePossible,

// 一个手势已经开始但尚未改变或者完成时

UIGestureRecognizerStateBegan,

// 手势状态改变

UIGestureRecognizerStateChanged,

// 手势完成

UIGestureRecognizerStateEnded,

// 手势取消,恢复至Possible状态

UIGestureRecognizerStateCancelled,

// 手势失败,恢复至Possible状态

UIGestureRecognizerStateFailed,

// 识别到手势识别

UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

};

需要同时支持多种手势时实现delegate方法,返回YES,可判断几种手势同时进行

// 该方法返回的BOOL值决定了view是否能够同时响应多个手势

- (BOOL)gestureRecognizer:(UIGestureRecognizer
*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

NSLog(@"%@ - %@", gestureRecognizer.class,
otherGestureRecognizer.class);

return YES;

}

1.点击识别器

// 创建点击手势识别器,并监听

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouch)];

// 设置手势识别器属性

// 点击几次响应

tap.numberOfTapsRequired = 2;

// 几个手指同时点击

tap.numberOfTouchesRequired = 2;

// 添加手势识别器

[self.uiimageView addGestureRecognizer:tap];

2.长按识别器

// 创建长按识别器,并监听

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressTouch)];

// 设置属性

// 多长时间响应

longPress.minimumPressDuration = 5;

// 按下后事件响应之前允许手指移动偏移位

longPress.allowableMovement = 10;

// 添加识别器

[self.view1 addGestureRecognizer:longPress];

3.轻扫

// 支持的手势方向枚举类型

typedef NS_OPTIONS(NSUInteger,
UISwipeGestureRecognizerDirection) {

UISwipeGestureRecognizerDirectionRight = 1 << 0, 
  // 向右

UISwipeGestureRecognizerDirectionLeft  = 1 << 1, 
  // 向左

UISwipeGestureRecognizerDirectionUp    = 1 << 2, 
  // 向上

UISwipeGestureRecognizerDirectionDown  = 1 << 3  
  // 向下

};

// 创建轻扫识别器,并监听如果要同时使用多个方向,分别创建多个轻扫识别器

// 例:添加向左向右手势

// 向左

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipehTouch)];

swipe.direction = UISwipeGestureRecognizerDirectionLeft;

[self.view1 addGestureRecognizer:swipe];

// 向右

UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipehTouch)];

swipe2.direction = UISwipeGestureRecognizerDirectionRight;

[self.view1 addGestureRecognizer:swipe2];

/**

* 旋转、捏合和拖拽注意点:当手指松开后会重置值,如果需要进行缩放旋转等操作,需在适当地方给返回的值赋值

*/

4.旋转

// 创建旋转识别器,并监听

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(RotationTouch:)];

[self.view1 addGestureRecognizer:rotation];

// 注意点

- (void)RotationTouch:(UIRotationGestureRecognizer
*)routation

{

NSLog(@"旋转");

// 每次加上之前的角度

self.view1.transform = CGAffineTransformRotate(self.view1.transform,
routation.rotation);

// 将旋转角度清0

routation.rotation = 0;

}

5.捏合

// 创建捏合手势

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchTouch:)];

[self.view1 addGestureRecognizer:pinch];

// 注意点

- (void)pinchTouch:(UIPinchGestureRecognizer
*)pinch

{

NSLog(@"捏合");

self.view1.transform = CGAffineTransformScale(self.view1.transform,
pinch.scale, pinch.scale);

pinch.scale = 1.0;

}

6.拖拽

// 创建拖拽手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanTouch)];

[self.view1 addGestureRecognizer:pan];

// 注意点

- (void)PanTouch:(UIPanGestureRecognizer *)pan

{

// 获取移动的坐标

CGPoint point = [pan translationInView:pan.view];

NSLog(@"拖拽");

CGPoint temp = self.view1.center;

temp.x += point.x;

temp.y += point.y;

self.view1.center = temp;

// 必须加

[pan setTranslation:CGPointZero inView:pan.view];

}

DEMO 链接: http://pan.baidu.com/s/1qW3VmUg 密码: 9pqk

上一篇:linux find


下一篇:#include