iOS开发之手势识别汇总
2015-07-03 10:01
197 查看
iOS开发中手势识别有六种:
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
更多iOS开发相关技术请关注iOS开发微信公众号 iOS开发 :
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
1,轻击手势(TapGestureRecognizer)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@se 4000 lector(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;//取消形变 }]; } }
更多iOS开发相关技术请关注iOS开发微信公众号 iOS开发 :
iOSDevTip
相关文章推荐
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- 基于Android中手势交互的实现方法
- iOS 使用手势UIGestureRecognizer对图像进行缩放、移动、旋转等操作
- iOS- UIImageView/手势识别
- 基于一次手势训练的kinect 3D 动态手势识别(提纲)
- 安卓,让edittext随手指移动而改变位置
- android中的手势探测
- 手势 gesture
- iOS——触摸事件与手势
- 利用长按手势移动TableView中的Cell
- android手势滑动——左右滑动效果实现
- UITouch 和 手势
- ios手势锁屏、解锁功能开发
- UI中的手势部分
- 几种手势操作的最基本实现
- android手势左右滑动
- iOS开发学习之触摸事件和手势识别
- 两个重叠组件的OnTouch事件冲突分析
- IOS开发—7种常用手势UIGestureRecognizer介绍
- JS_Canvas_GesturesUnlock 手势解锁 v1.0