UIGestureRecognizer,手势处理,点击,长按,拖动,左右划动
2015-12-27 10:03
501 查看
对于移动端,由于用户都是通过手指触摸来操作的,所以对于手势的处理是必不可少的。下面详细谈谈有哪几种手势操作。
1.点击,长按,拖动,左划,右划(上划下划)。
2.闲话不多说,直接看代码。代码Demo地址:https://github.com/gujinyue1010/iOS_UIGestureRecognizer
1.点击,长按,拖动,左划,右划(上划下划)。
2.闲话不多说,直接看代码。代码Demo地址:https://github.com/gujinyue1010/iOS_UIGestureRecognizer
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView1; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.imageView1.userInteractionEnabled=YES; //1.点击手势 //创建手势实例 UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture)]; //设置手势点击数(不设置默认是一次) //tapGesture.numberOfTapsRequired=2; //为imageView1添加手势识别 [self.imageView1 addGestureRecognizer:tapGesture]; //2.长按手势 UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture)]; longPressGesture.minimumPressDuration=1.0; [self.imageView1 addGestureRecognizer:longPressGesture]; //3.拖手势 UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]; [self.imageView1 addGestureRecognizer:panGesture]; //4.左划手势(整个屏幕给个划动手势) UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeLeftGesture]; //5.右划手势 UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRightGesture]; } -(void)handleTapGesture { NSLog(@"双击了图片"); } -(void)handleLongPressGesture { NSLog(@"长按了图片"); } -(void)handlePanGesture:(UIPanGestureRecognizer *)sender { //平衡 CGPoint netTranslation; //得到拖过程中的xy坐标 CGPoint translation=[(UIPanGestureRecognizer *)sender translationInView:self.imageView1]; //平移图片 sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y +translation.y); if(sender.state==UIGestureRecognizerStateEnded) { netTranslation.x+=translation.x; netTranslation.y+=translation.y; } NSLog(@"在拖动"); } -(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender { NSLog(@"%lu",(unsigned long)sender.direction); if(sender.direction==UISwipeGestureRecognizerDirectionUp) { NSLog(@"向上"); } else if(sender.direction==UISwipeGestureRecognizerDirectionLeft) { NSLog(@"向左"); } else if(sender.direction==UISwipeGestureRecognizerDirectionDown) { NSLog(@"向下"); } else if(sender.direction==UISwipeGestureRecognizerDirectionRight) { NSLog(@"向右"); } }
相关文章推荐
- 据说是Google首页的网页模块拖动代码
- 轻松学习jQuery插件EasyUI EasyUI实现拖动基本操作
- JAVASCRIPT 点击显示 隐藏层
- javascript实现淡蓝色的鼠标拖动选择框实例
- JS实现超简单的鼠标拖动效果
- javascript实现的元素拖动函数宿主为浏览器
- javascript弹出拖动窗口
- JS拖动鼠标画出方框实现鼠标选区的方法
- javascript点击才出现验证码
- JS实现三个层重叠点击互相切换的方法
- javascript实现简单的鼠标拖动效果实例
- js简单实现点击左右运动的方法
- jQuery通过点击行来删除HTML表格行的实现示例
- jQuery EasyUI Dialog拖不下来如何解决
- jquery移动点击的项目到列表最顶端的方法
- jquery实现点击变换导航样式的方法
- PHP+MySQL+jQuery随意拖动层并即时保存拖动位置实例讲解
- jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
- JQuery表格拖动调整列宽效果(自己动手写的)
- jQuery实现仿Google首页拖动效果的方法