博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发中六种手势识别
阅读量:4589 次
发布时间:2019-06-09

本文共 3628 字,大约阅读时间需要 12 分钟。

iOS开发中手势识别有六种: 

轻击手势(TapGestureRecognizer), 

轻扫手势 (SwipeGestureRecognizer), 

长按手势(LongPressGestureRecognizer), 

拖动手势(PanGestureRecognizer), 

捏合手势(PinchGestureRecognizer), 

旋转手势(RotationGestureRecognizer), 

1,轻击手势(TapGestureRecognizer) 

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];tapGesture.numberOfTapsRequired = 1; //点击次数tapGesture.numberOfTouchesRequired = 1; //点击手指数[self.view addGestureRecognizer:tapGesture];//轻击手势触发方法-(void)tapGesture:(UITapGestureRecognizer *)sender{    //your code}

2,长按手势(LongPressGestureRecognizer) 

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];//设置长按时间longPressGesture.minimumPressDuration = 0.5;[self.view addGestureRecognizer:longPressGesture];//长按手势触发方法-(void)longPressGesture:(id)sender{	 UILongPressGestureRecognizer *longPress = sender;	 if (longPress.state == UIGestureRecognizerStateBegan)	 {		 //your code	 }}说明:长按手势的常用状态如下开始:UIGestureRecognizerStateBegan改变:UIGestureRecognizerStateChanged结束:UIGestureRecognizerStateEnded取消:UIGestureRecognizerStateCancelled失败:UIGestureRecognizerStateFailed

3,轻扫手势(SwipeGestureRecognizer) 

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右[self.view addGestureRecognizer:swipeGesture];UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左[self.view addGestureRecognizer:swipeGestureLeft];//轻扫手势触发方法-(void)swipeGesture:(id)sender{	UISwipeGestureRecognizer *swipe = sender;	if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)	{		//向左轻扫	}	if (swipe.direction == UISwipeGestureRecognizerDirectionRight)	{		//向右轻扫	}}

4,捏合手势(PinchGestureRecognizer) 

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture];	捏合手势触发方法-(void) pinchGesture:(id)sender{	UIPinchGestureRecognizer *gesture = sender;	//手势改变时	if (gesture.state == UIGestureRecognizerStateChanged)	{		 //捏合手势中scale属性记录的缩放比例		_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);	}	//结束后恢复	if(gesture.state==UIGestureRecognizerStateEnded)	{		[UIView animateWithDuration:0.5 animations:^{			_imageView.transform = CGAffineTransformIdentity;//取消一切形变		}];	}}

5,拖动手势(PanGestureRecognizer) 

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];//拖动手势触发方法-(void) panGesture:(id)sender{    UIPanGestureRecognizer *panGesture = sender;    CGPoint movePoint = [panGesture translationInView:self.view];    //your code}

6,旋转手势(RotationGestureRecognizer) 

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];//旋转手势触发方法-(void)rotationGesture:(id)sender{	UIRotationGestureRecognizer *gesture = sender;	if (gesture.state==UIGestureRecognizerStateChanged)	{		_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);	}	if(gesture.state==UIGestureRecognizerStateEnded)	{		[UIView animateWithDuration:1 animations:^{			_imageView.transform=CGAffineTransformIdentity;//取消形变		}];	}}

转载于:https://www.cnblogs.com/ming1025/p/6139851.html

你可能感兴趣的文章
Django----模板
查看>>
如何将当前时间与已设时间比较大小
查看>>
电子书下载:Silverlight 5 in Action
查看>>
Day50 python 多表操作
查看>>
WPF与WinForm开发有什么区别?
查看>>
Python中用format函数格式化字符串
查看>>
黑马程序员——函数
查看>>
java开发环境配置(win8 64位)
查看>>
其中考试
查看>>
(8)zabbix监控项item是什么
查看>>
本人的coding地址
查看>>
json_encode
查看>>
洛谷 1164 小A点菜
查看>>
客户端连接服务端的配置文件
查看>>
【POJ - 1995】Raising Modulo Numbers(快速幂)
查看>>
python model对象转为dict数据
查看>>
RPC
查看>>
sql 转 markdown
查看>>
UI自动化笔记(二)
查看>>
WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接...
查看>>