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

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

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

轻击手势(TapGestureRecognizer),

轻扫手势(SwipeGestureRecognizer),

长按手势(LongPressGestureRecognizer),

拖动手势(PanGestureRecognizer),

捏合手势(PinchGestureRecognizer),

旋转手势(RotationGestureRecognizer),

1,轻击手势(TapGestureRecognizer)

UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGesture:)];

tapGesture.numberOfTapsRequired=1;//点击次数

tapGesture.numberOfTouchesRequired=1;//点击手指数

[self.viewaddGestureRecognizer:tapGesture];

//轻击手势触发方法

-(void)tapGesture:(UITapGestureRecognizer*)sender

{

//yourcode

}

2,长按手势(LongPressGestureRecognizer)

UILongPressGestureRecognizer*longPressGesture=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressGesture:)];

//设置长按时间

longPressGesture.minimumPressDuration=0.5;

[self.viewaddGestureRecognizer:longPressGesture];

//长按手势触发方法

-(void)longPressGesture:(id)sender

{

UILongPressGestureRecognizer*longPress=sender;

if(longPress.state==UIGestureRecognizerStateBegan)

{

//yourcode

}

}

说明:长按手势的常用状态如下

开始:UIGestureRecognizerStateBegan

改变:UIGestureRecognizerStateChanged

结束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失败:UIGestureRecognizerStateFailed

3,轻扫手势(SwipeGestureRecognizer)

UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeGesture:)];

//设置轻扫的方向

swipeGesture.direction=UISwipeGestureRecognizerDirectionRight;//向右

[self.viewaddGestureRecognizer:swipeGesture];

UISwipeGestureRecognizer*swipeGestureLeft=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeGesture:)];

//设置轻扫的方向

swipeGestureLeft.direction=UISwipeGestureRecognizerDirectionLeft;//向左

[self.viewaddGestureRecognizer:swipeGestureLeft];

//轻扫手势触发方法

-(void)swipeGesture:(id)sender

{

UISwipeGestureRecognizer*swipe=sender;

if(swipe.direction==UISwipeGestureRecognizerDirectionLeft)

{

//向左轻扫

}

if(swipe.direction==UISwipeGestureRecognizerDirectionRight)

{

//向右轻扫

}

}

4,捏合手势(PinchGestureRecognizer)

UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchGesture:)];

[self.viewaddGestureRecognizer:pinchGesture];

捏合手势触发方法

-(void)pinchGesture:(id)sender

{

UIPinchGestureRecognizer*gesture=sender;

//手势改变时

if(gesture.state==UIGestureRecognizerStateChanged)

{

//捏合手势中scale属性记录的缩放比例

_imageView.transform=CGAffineTransformMakeScale(gesture.scale,gesture.scale);

}

//结束后恢复

if(gesture.state==UIGestureRecognizerStateEnded)

{

[UIViewanimateWithDuration:0.5animations:^{

_imageView.transform=CGAffineTransformIdentity;//取消一切形变

}];

}

}

5,拖动手势(PanGestureRecognizer)

UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panGesture:)];

[self.viewaddGestureRecognizer:panGesture];

//拖动手势触发方法

-(void)panGesture:(id)sender

{

UIPanGestureRecognizer*panGesture=sender;

CGPointmovePoint=[panGesturetranslationInView:self.view];

//yourcode

}

6,旋转手势(RotationGestureRecognizer)

UIRotationGestureRecognizer*rotationGesture=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationGesture:)];

[self.viewaddGestureRecognizer:rotationGesture];

//旋转手势触发方法

-(void)rotationGesture:(id)sender

{

UIRotationGestureRecognizer*gesture=sender;

if(gesture.state==UIGestureRecognizerStateChanged)

{

_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);

}

if(gesture.state==UIGestureRecognizerStateEnded)

{

[UIViewanimateWithDuration:1animations:^{

_imageView.transform=CGAffineTransformIdentity;//取消形变

}];

}

}

转载地址:http://gquta.baihongyu.com/

你可能感兴趣的文章
sql server 实现多表连接查询
查看>>
HTTP 1.1与HTTP 1.0的比较
查看>>
如何在命令行脚本中启动带参数的Windows服务
查看>>
abstract vs interface
查看>>
nodejs笔记1 ----关于express不是本地命令
查看>>
python debug
查看>>
docker-machine 远程安装docker
查看>>
最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
查看>>
3.2 进程间通信之fifo
查看>>
WEB 开发前传——js笔记
查看>>
C++多线程编程入门(转)
查看>>
C# to IL 6 Reference and Value Types(引用类型和值类型)
查看>>
Python 爬虫十六式 - 第七式:正则的艺术
查看>>
Android Studio的使用(五)--导入第三方Jar包
查看>>
PCL学习笔记(一)
查看>>
CoreGraphics相关方法
查看>>
Node Express 初探
查看>>
回溯法-求解装载问题(类似0-1背包)
查看>>
MATLAB中求矩阵非零元的坐标
查看>>
Tabular系列之问题1:如何利用其他人的账号进行权限测试?
查看>>