iOS手势识别的简单应用
2016-06-22 11:15
459 查看
摘要: UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)
六种手势
1.手势分类UITapGestureRecognizer(敲击)设置numberOfTapsRequired点击的次数结合代理(BOOL) gestureRecognizer可以选定可点击的区域UIPinchGestureRecognizer(捏合,用于缩放)注意复位问题UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)长按的两次响应问题的解决2.对每一个手势的注意点及代码实现进行说明UITapGestureRecognizer(敲击)#pragma mark - tap敲击 /** * 1.numberOfTapsRequired设置点击几次响应 * 2.代理与(BOOL) gestureRecognizer结合,可确定图片哪个区域可以点,哪些不能点 */ -(void) setTap{ //创建点按手势 //initWithTarget:谁监听这个事件呢,一般控制器 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; //delegate tap.delegate=self; //click counts tap.numberOfTapsRequired=2; //添加手势,用的是多态addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer(父类) [self.imageView addGestureRecognizer:tap]; } -(void) tap:(UITapGestureRecognizer *) tap{ NSLog(@"%s",__func__); }#pragma mark - UIGestureRecognizerDelegate//与tap结合-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch )touch{//获取当前触摸点在self.imageView的哪个位置CGPoint curP=[touch locationInView:self.imageView];if (curP.x<self.imageView.bounds.size.width0.5) {return YES;}else{return NO;}}
```UIPinchGestureRecognizer(捏合,用于缩放)#pragma mark - pinch捏合,用于缩放-(void) setPinch{UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];pinch.delegate=self;[self.imageView addGestureRecognizer:pinch];}-(void) pinch:(UIPinchGestureRecognizer *) pinch{NSLog(@"%s",func);self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);pinch.scale=1;}```UIPanGestureRecognizer(拖拽)
#pragma mark - pan拖拽 /** * 复位是指返回到起始的状态,再能过相对上一个状态tansform来解决一些问题 */ -(void) setPan{ UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:pan]; } -(void) pan:(UIPanGestureRecognizer *) pan{ NSLog(@"%s",__func__); // CGPoint curP=[pan locationInView:self.imageView]; //获取图片移动距离 CGPoint curP=[pan translationInView:self.imageView]; self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y); //复位 [pan setTranslation:CGPointZero inView:self.imageView]; }UISwipeGestureRecognizer(轻扫)
#pragma mark - swipe轻扫 -(void) setSwipe{ //添加向左滑动 UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; swipe.direction=UISwipeGestureRecognizerDirectionLeft; [self.imageView addGestureRecognizer:swipe]; //添加向右滑动 UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)]; swipeDown.direction=UISwipeGestureRecognizerDirectionRight; [self.imageView addGestureRecognizer:swipeDown]; } -(void) swipe:(UISwipeGestureRecognizer *)swipe{ NSLog(@"%s",__func__); }UIRotationGestureRecognizer(旋转)
#pragma mark - rotation旋转 -(void) setRotation{ UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; rotation.delegate=self; [self.imageView addGestureRecognizer:rotation]; } -(void) rotation:(UIRotationGestureRecognizer *) rotation{ NSLog(@"%s",__func__); NSLog(@"%f",rotation.rotation); self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation); [rotation setRotation:0]; // rotation.rotation=0; }UILongPressGestureRecognizer(长按)
#pragma mark - longPress长按 -(void) setLongPress{ UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; [self.imageView addGestureRecognizer:longPress]; } /** * 长按的一些注意点,那就是长按时触发一次,离开时再触发一次 * 加个判断,结束与开始选一个触发的时间 * @param longPress 传参 */ -(void) longPress:(UILongPressGestureRecognizer *) longPress{ if (longPress.state==UIGestureRecognizerStateBegan) { NSLog(@"%s",__func__); } }当同时多个手势可以处理时(代理)/**是否支持多个手势同时处理@param gestureRecognizer 。。@param otherGestureRecognizer 。。@return return value bool*/-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES;}3.源代码的地址github地址
相关文章推荐
- iOS10怎么升级?iOS10 beta1升级教程
- iOS 开发之Target-action模式
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- iOS快速集成支付宝(完善版)
- AVFoundation的介绍
- iOS语音播放之切换听筒和扬声器的方法解决方案
- iOS常用的两种延时的方法
- IOS中Json解析实例方法详解(四种方法)
- iOS 数据存储的常用方式
- iOS 持续集成- Mac下命令启动和关闭Jenkins
- iOS 时间戳转化为时间
- iOS 把当前时间转化为时间戳
- iOS真机调试问题The certificate has either expired or has been revoked
- CocoaPods 使用时候的问题
- IOS应用内跳转系统设置相关界面的方法
- iOS通过html模版实现富文本编辑
- ios 动画学习的套路 (二)
- IOS热更新-JSPatch实现原理+Patch现场恢复
- JSBridge(Android和IOS平台)的设计和实现
- 转场动画