手势识别器
2015-08-06 09:05
381 查看
在实现手势识别器之前 需要建立 一个UIImageView来存放图片
原代码:
手势的使用
设置长按触发的最小时间(默认是0.5)
longPress.minimumPressDuration =0.5;
用户手指在长按过程中允许移动的距离(默认是10)
longPress.allowableMovement =200;
把手势添加到图片中
[self.imageView addGestureRecognizer:longPress];
[longPress release];
#pragma mark 长按对应的响应方法
-(void)longPressClick:(UILongPressGestureRecognizer *)longPress{
NSLog(@”测试长按手势”);
// 长按的状态
// longPress.state;
长按之后弹出一个UIAlertView
if(self.alertView == nil){
self.alertView =[[UIAlertView alloc] initWithTitle:@”长按” message:@”进入” delegate:self cancelButtonTitle:@”确定” otherButtonTitles:@”取消”, nil];
}
[self.alertView show];
[self.alertView release];
self.alertView =nil;
}
5.拖拽
6.轻扫
原代码:
UIImage *image =[UIImage imageNamed:@"图片名.后缀"]; self.imageView =[[UIImageView alloc ] initWithImage:image]; self.imageView.frame =CGRectMake(30, 100, 300, 400); [self.view addSubview:self.imageView]; [self.imageView release];
手势的使用
1.点击
原代码:UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; 设置点击几次才能触发方法(默认是1) tap.numberOfTapsRequired =2; 设置几根手指进行点击(默认是1) tap.numberOfTouchesRequired =1; 将手势添加到图片上 [self.imageView addGestureRecognizer:tap]; [tap release]; #pragma mark 点击的方法 -(void)tapClick:(UITapGestureRecognizer *)tap{ NSLog(@"测试一下手势"); self.imageView.image =[UIImage imageNamed:@"7af40ad162d9f2d3efbd7dd2abec8a136327cc5b.jpg"]; }
2.长按
UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];设置长按触发的最小时间(默认是0.5)
longPress.minimumPressDuration =0.5;
用户手指在长按过程中允许移动的距离(默认是10)
longPress.allowableMovement =200;
把手势添加到图片中
[self.imageView addGestureRecognizer:longPress];
[longPress release];
#pragma mark 长按对应的响应方法
-(void)longPressClick:(UILongPressGestureRecognizer *)longPress{
NSLog(@”测试长按手势”);
// 长按的状态
// longPress.state;
长按之后弹出一个UIAlertView
if(self.alertView == nil){
self.alertView =[[UIAlertView alloc] initWithTitle:@”长按” message:@”进入” delegate:self cancelButtonTitle:@”确定” otherButtonTitles:@”取消”, nil];
}
[self.alertView show];
[self.alertView release];
self.alertView =nil;
}
3.旋转
创建一个旋转的手势 UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)]; 把手势放到对应的图片上 [self.imageView addGestureRecognizer:rotation]; 释放 [rotation release]; #pragma mark 旋转对应的响应方法(通过图片的手势,让图片发生旋转) -(void)rotationClick:(UIRotationGestureRecognizer *)rotation{ // 可以通过手势获取手势添加的是图示是哪一个 UIImageView *imageView = (UIImageView *)rotation.view; // 进行旋转的操作 // 通过视图的transfrom属性,让视图进行旋转 // imageView.transform =CGAffineTransformMakeRotation(rotation.rotation); imageView.transform =CGAffineTransformRotate(imageView.transform, rotation.rotation); // 防止转的太快,斗; rotation.rotation =0; }
4.捏合
创建 UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc ] initWithTarget:self action:@selector(pinchClick:)]; 添加到图片上 [self.imageView addGestureRecognizer:pinch]; 释放 [pinch release]; #pragma mark 捏合对应的响应方法 (通过捏合手势,缩放图片) -(void)pinchClick:(UIPinchGestureRecognizer *)pinch{ // 通过手势找到对应的视图 UIImageView *imageView =(UIImageView *)pinch.view; // 进行捏合的操作 // 通过transform改变图片的尺寸 imageView.transform =CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale); // 为了防止手势的变化让图片直接消失(停止动作) pinch.scale =1; }
5.拖拽
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)]; [self.imageView addGestureRecognizer:pan]; [pan release]; #pragma mark 拖拽对应的响应方法(通过拖拽手势,让视图随着手势移动) -(void)panClick:(UIPanGestureRecognizer *)pan{ // 通过手势找视图 UIImageView *imageView =(UIImageView *)pan.view; // 通过手势获得经过的点 // imageView.transform =CGAffineTransformMakeScale(pan., <#CGFloat sy#>) CGPoint p=[pan translationInView:imageView]; //设置移动的位置 imageView.transform =CGAffineTransformTranslate(imageView.transform, p.x, p.y); // 为了防止手势在操作的时候视图消失 [pan setTranslation:CGPointZero inView:imageView]; }
6.轻扫
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)]; [self.imageView addGestureRecognizer:swipe]; [swipe release]; 轻扫的方向 向右 swipe.direction =UISwipeGestureRecognizerDirectionRight; # pragma mark 轻扫对应的响应 -(void)swipeClick:(UISwipeGestureRecognizer *)swipe{ if (swipe.direction ==UISwipeGestureRecognizerDirectionRight ) { NSLog(@"向右"); } }
相关文章推荐
- android使用gesturedetector手势识别示例分享
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- 基于Android中手势交互的实现方法
- IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)
- 在Swift程序中实现手势识别的方法
- IOS初级教程2:UITapGestureRecognizer手势识别的简单使用
- Android Gesture Detector
- iOS 使用手势UIGestureRecognizer对图像进行缩放、移动、旋转等操作
- 导航控制器 手势识别不了
- iOS- UIImageView/手势识别
- 基于一次手势训练的kinect 3D 动态手势识别(提纲)
- IOS 开发手势识别
- 转自原文地址:IOS UIScrollview UIImageView点击事件处理
- 安卓,让edittext随手指移动而改变位置
- ios自用学习笔记-触摸事件与手势识别
- 安卓(Android)surfaceview无法使用gesturedetector的解决办法
- android中的手势探测
- OC笔记 - 事件处理(2015.4.3)
- 手势识别器
- 一个比较有前景的电脑应用程序构想