如何区分ios上的pan和swipeios pan手势左右滑动

iOS开发之手势识别-值得收藏_iOS开发-爱微帮
&& &&& iOS开发之手势识别-值得收藏
总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer), 拖动手势(PanGestureRecognizer), 捏合手势(PinchGestureRecognizer),旋转手势(RotationGestureRecognizer);1,轻击手势(TapGestureRecognizer)UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];tapGesture.numberOfTapsRequired = 1; tapGesture.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tapGesture];-(void)tapGesture:(id)sender{ }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 = if (longPress.state == UIGestureRecognizerStateBegan) { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@&提示& message:@&长按触发& delegate:nil cancelButtonTitle:@&取消& otherButtonTitles: nil]; [alter show]; }}说明:手势的常用状态如下开始:UIGestureRecognizerStateBegan改变:UIGestureRecognizerStateChanged结束:UIGestureRecognizerStateEnded取消:UIGestureRecognizerStateCancelled失败:UIGestureRecognizerStateFailed3,轻扫手势(SwipeGestureRecognizer)UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];swipeGesture.direction = UISwipeGestureRecognizerDirectionR [self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionL [self.view addGestureRecognizer:swipeGestureLeft];-(void)swipeGesture:(id)sender{ UISwipeGestureRecognizer *swipe = 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 =
if (gesture.state == UIGestureRecognizerStateChanged) {
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale); }
if(gesture.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:0.5 animations:^{ _imageView.transform = CGAffineTransformI }]; }}5,拖动手势(PanGestureRecognizer)UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];-(void) panGesture:(id)sender{ UIPanGestureRecognizer *panGesture =
CGPoint movePoint = [panGesture translationInView:self.view];
}6,旋转手势(RotationGestureRecognizer)UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];-(void)rotationGesture:(id)sender{
UIRotationGestureRecognizer *gesture =
if (gesture.state==UIGestureRecognizerStateChanged) { _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation); }
if(gesture.state==UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:1 animations:^{ _imageView.transform=CGAffineTransformI }]; }
}●本文编号1,以后想阅读这篇文章直接输入1即可。●本文分类“手势”,直接回复分类名可以获得相关文章。如果你觉得iOS开发公众微信对你有帮助,请点击右上角“...”标志分享到【朋友圈】,感谢支持!PhonePlus (ID: PhoneMost )(←长按复制) PhoenPlus为您讲述苹果设备、iPhone手机、iOS系统、智能设备的资讯、技巧和故事。
点击展开全文
悄悄告诉你
更多同类文章
还可知道有多少人阅读过此篇文章哦
阅读原文和更多同类文章
可微信扫描右侧二维码关注后
还可知道有多少人阅读过此篇文章哦
最新iOS、iPhone资讯,万名iOS开发者、swift开发、果粉聚集,参与技术讨论,整理开发技巧,分享创业经验!享受生活、热爱编程!
您的【关注和订阅】是作者不断前行的动力
本站文章来自网友的提交收录,如需删除可进入
删除,或发送邮件到 bang@ 联系我们,
(C)2014&&版权所有&&&|&&&
京ICP备号-2&&&&京公网安备34  在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图
 UIKit框架提供了一些预定义的GestureRecognizer.包含下列手势
 UITapGestureRecognizer敲击手势(单击和双击)
 UIPanGestureRecognizer(拖动手势)
 UIPinchGestureRecognizer(缩放手势)
 UISwipeGestureRecognizer(擦碰手势)
 UIRotationGestureRecognizer(旋转手势)
 UILongPressGestureRecognizer(长按手势)
如果你想让你的应用程序来识别一个独特的手势,如选择目录或纠结的运动,你可以创建自己的自定义GestureRecognizer,将在下篇介绍
将特定的手势和view相关联
  每一个特定的手势必须关联到view对象中才会有作用,一个view对象可以关联多个不同的特定手势,但是每一个特定的手势只能与一个view相关联。当用户触摸了view,这个GestureRecognizer就会接受到消息,它可以响应特定的触摸事件。
与特定view关联
创建GestureRecognizer实例
addGestureRecognizer
实现处理手势的方法
可以使用removeGestureRecognizer: 来移除手势。
1 _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlerPanGesture:)];
_panGestureRecognizer.delegate =
_panGestureRecognizer.maximumNumberOfTouches = 2;
_panGestureRecognizer.minimumNumberOfTouches = 2;
[self.view addGestureRecognizer:_panGestureRecognizer];
7 - (void)handlerPanGesture:(UIPanGestureRecognizer *)recognizer
if ((recognizer.state == UIGestureRecognizerStateBegan) ||
(recognizer.state == UIGestureRecognizerStateChanged))
CGPoint offset = [recognizer translationInView:self.view];
CGRect frame = self.rightViewController.view.
frame.origin.x += offset.x;
if (frame.origin.x &= 0 && frame.origin.x &= kScreenWidth)
self.rightViewController.view.frame =
[recognizer setTranslation:CGPointZero inView:self.view];
else if (recognizer.state == UIGestureRecognizerStateEnded)
BOOL isVisible = self.rightViewController.view.frame.origin.x & kScreenWidth / 2;
[self showRightView:isVisible];
手势识别状态
Gesture recognizers从一个状态转到另一状态(state)。对于每个状态,根据它们是否符合特定条件来决定时候可以移动到下一个状态。它们分析多点触摸。是否识别失败。未能识别手势意味着state 转换失败。UIGestureRecognizerStateFailed。详见UIGestureRecognizerState枚举
1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible,
// the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan,
// the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged,
// the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded,
// the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled,
// the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed,
// the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures & gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
为view添加多个手势
  当一个view添加多个手势时,在缺省情况下,没有为优先执行哪个手势做排序,每次发生不同。不过你可以覆盖默认的行为(使用类方法、委托方法、和子类化覆盖这些)
指定一个Gesture recognizers应该在另一个前捕捉。
requireGestureRecognizerToFail: 这个方法就是在作为参数的Gesture recognizer失败以后接受者才发生,否则从不会发生。
[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];
允许2个手势同时操作
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
禁止在某一点发生Gesture recognizers
1 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
if ([touch.view isKindOfClass:[UIControl class]])
return NO;
return YES;
指定一个单向关系两个手势识别器
想控制两个识别器相互作用,但你需要指定一个单向关系,您可以重写或canPreventGestureRecognizer:或canBePreventedByGestureRecognizer:子类方法。return yes。例如,如果你想要一个旋转的姿态来防止捏动作,但你不想夹手势防止旋转的姿态。例如,你想一个旋转手势阻止一个缩放手势,但你不想一个缩放手势阻止旋转手势,就加入下面代码
[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];
阅读(...) 评论()trackbacks-0
& & & 在iPhone中,我们除了用touchesBegan、touchesMoved、touchesEnded、touchesCancelled这组方法来控制使用者的手指触控外,也可以用UIGestureRecognizer 的衍生类别来进行判断,该类别的好处是有自己现成的手势,开发者可以自己计算手指移动的轨迹。
&1、首先看下touchesBegan、touchesMoved、touchesEnded、touchesCancelled者四个方法的使用。
& & touchesBegan:是用户第一次接触屏幕时响应的方法。
& & - (void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *)event{
& & & & &NSUInteger numType = [[touches anyObject] tapCount];
& & & & &NSUInteger numTouches = [touches count];
& & touchesMoved:是用户手指移过屏幕时响应的方法。这里可以得到当前手指的位置以及原来的位置等信息。
& & touchesEnded:用户手指离开屏幕时响应的方法。
& & touchesCancelled:当发生某些事件导致手势中断时响应的方法,要说明的是调用了该方法就不会响应touchesEned方法了。
2、现在了解下UIGestureRecognizer的使用
& & UIGestureRecognizer衍生类别有:UITapGestureRecognizer、UIPinchGestureRecognizer、UIRotationGestureRecognizer、UISwipeGestureRecognizer、UIPanGEstureRecognizer、UiLongPressGestureRecognizer,他们所代表的手势分别是Tap(点一下)、Pinch(二指往外或往内拨动)、Rotation(旋转)、Swipe(滑动、快速移动)、Pan(拖移、慢速移动)、LongPress(长时间按)。
& & -&(void)viewDidLoad&{ && &
    UISwipeGestureRecognizer*& && &&
    &&& &
     recognizer&=&[[UISwipeGestureRecognizer&alloc]&initWithTarget:selfaction:@selector(handleSwipeFrom)]; && &&
    &&& &&
    &&& &&
    &&& &
    recognizer.direction&=&UISwipeGestureRecognizerDirectionUp && &
    [self.view&addGestureRecognizer:recognizer]; && &
    [recognizer&release]; &
  &} && &&
  -&(void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer&{ && &
    &&&& &&
    &&&&& &
     [self.view&removeGestureRecognizer:recognizer];
& & & 但是这里有个问题,Tap和LongPress,一个是点击一次一个长按,所以当我们点击屏幕的时候,我们怎么分清这是点击还是准备长按呢?Swipe和Pan也有这样的问题,只要手指一移动就会出发Pan,这样的话永远不会出发Swipe,这个问题要出现的前提是同时注册了这些手势。
& & &所以需要一个解决方法:幸好UIGestureRecognizer类提供了一个方法requireGestureRecognizerToFail它可以指定某一个recognizer,即便自己满足条件; 也不会立即执行会等到指定的recognizer确定失败之后才触发。
  -&(void)viewDidLoad&{ && &
    &&& &
    UITapGestureRecognizer*&singleR &
    singleRecognizer&=&[[UITapGestureRecognizer&alloc]&initWithTarget:selfaction:@selector(handleSingleTapFrom)];
& &   &&singleTapRecognizer.numberOfTapsRequired&=&1;&
    &&&
     [self.view&addGestureRecognizer:singleRecognizer]; &&
    &&& &
    UITapGestureRecognizer*&doubleR &
    doubleRecognizer&=&[[UITapGestureRecognizer&alloc]&initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
  && & &doubleTapRecognizer.numberOfTapsRequired&=&2;
    &&& &
    [self.view&addGestureRecognizer:doubleRecognizer]; && & &&&
    &&&
    [singleRecognizer&requireGestureRecognizerToFail:doubleRecognizer]; &&
    [singleRecognizer&release]; &
    [doubleRecognizer&release];
阅读(...) 评论()<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&今天看啥 热点:
iOS开发之手势识别,ios开发手势  感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,也是目标动作回调,话不多说,切入今天的正题。总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势(SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer), &拖动手势(PanGestureRecognizer), 捏合手势(PinchGestureRecognizer),旋转手势(RotationGestureRecognizer);
  其实这些手势用touche事件完全可以实现,苹果就是把常用的触摸事件封装成手势,来提供给用户。读者完全可以用TouchesMoved来写拖动手势等
  一,用storyboard给控件添加手势识别,当然啦用storyboard得截张图啦
    1.用storyboard添加手势识别,和添加一个Button的步骤一样,首先我们得找到相应的手势,把手势识别的控件拖到我们要添加手势的控件中,截图如下:
  二,纯代码添加手势识别
    用storyboard可以大大简化我们的操作,不过纯代码的方式还是要会的,就像要Dreamwear编辑网页一样(当然啦,storyboard的拖拽功能要比Dreamwear的拖拽强大的多),用纯代码敲出来的更为灵活,更加便于维护。不过用storyboard可以减少我们的工作量,这两个要配合着使用才能大大的提高我们的开发效率。个人感觉用storyboard把框架搭起来(Controller间的关系),一下小的东西还是用纯代码敲出来更好一些。下面就给出如何给我们的控件用纯代码的方式来添加手势识别。
    1.轻击手势(TapGestureRecognizer)的添加
      初始化代码TapGestureRecongnizer的代码如下:
//新建tap手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置点击次数和点击手指数
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
[self.view addGestureRecognizer:tapGesture];
    在回调方法中添加相应的业务逻辑:
1 //轻击手势触发方法
2 -(void)tapGesture:(id)sender
//轻击后要做的事情
    2.长按手势(LongPressGestureRecognizer)
      初始化代码:
//添加长摁手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5; //(2秒)
[self.view addGestureRecognizer:longPressGesture];
     在对应的回调方法中添加相应的方法(当手势开始时执行):
1 //常摁手势触发方法
2 -(void)longPressGesture:(id)sender
UILongPressGestureRecognizer *longPress =
if (longPress.state == UIGestureRecognizerStateBegan)
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
[alter show];
    代码说明:手势的常用状态如下
      开始:UIGestureRecognizerStateBegan
      改变:UIGestureRecognizerStateChanged
      结束:UIGestureRecognizerStateEnded
      & 取消:UIGestureRecognizerStateCancelled
      失败:UIGestureRecognizerStateFailed
    3.轻扫手势(SwipeGestureRecognizer)
      在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。
      添加轻扫手势,一个向左一个向右,代码如下:
//添加轻扫手势
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionR //默认向右
[self.view addGestureRecognizer:swipeGesture];
//添加轻扫手势
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionL //默认向右
[self.view addGestureRecognizer:swipeGestureLeft];
     回调方法如下:
1 //轻扫手势触发方法
2 -(void)swipeGesture:(id)sender
UISwipeGestureRecognizer *swipe =
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
//向左轻扫做的事情
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
//向右轻扫做的事情
    4.捏合手势(PinchGestureRecognizer)
      捏合手势初始化
//添加捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
      捏合手势要触发的方法(放大或者缩小图片):
1 ////捏合手势触发方法
2 -(void) pinchGesture:(id)sender
UIPinchGestureRecognizer *gesture =
//手势改变时
if (gesture.state == UIGestureRecognizerStateChanged)
//捏合手势中scale属性记录的缩放比例
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
//结束后恢复
if(gesture.state==UIGestureRecognizerStateEnded)
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformI//取消一切形变
    5.拖动手势(PanGestureRecognizer)
      拖动手势的初始化
//添加拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
    拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)
1 //拖动手势
2 -(void) panGesture:(id)sender
UIPanGestureRecognizer *panGesture =
CGPoint movePoint = [panGesture translationInView:self.view];
//做你想做的事儿
    6.旋转手势(RotationGestureRecognizer)
      旋转手势的初始化
//添加旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
      旋转手势调用的方法:
1 //旋转手势
2 -(void)rotationGesture:(id)sender
UIRotationGestureRecognizer *gesture =
if (gesture.state==UIGestureRecognizerStateChanged)
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
if(gesture.state==UIGestureRecognizerStateEnded)
[UIView animateWithDuration:1 animations:^{
_imageView.transform=CGAffineTransformI//取消形变
  上面的东西没有多高深的技术,就是对iOS开发中的手势做了一下小小的总结,温故一下基础知识。在之前的博客中也有用到手势识别的内容,就是没有系统的梳理一下手势识别的知识,本篇博客做一个基础的补充吧。欢迎批评指正,转载请注明出处。
可以。[sender view]就能得到当前调用的view。
iOS 5最近已经更新到了Beta 3。在该版本中,开发者可以通过手势来实现各种操作,这也是iOS 5 Beta 3版最大的亮点和改进。目前开发者可使用简单的单点触控手势和比较复杂多点触控手势,通过这些手势可以实现锁屏、调节音量以及与Swipe类似的滑屏手势输入。 iOS 5手势操作设置 iOS 5手势操作设置 iOS 5手势操作设置 iOS 5多点触控手势操作 手势操作原本在PC端的网页浏览器应用得比较多,其雏形是“鼠标手势”。近几年智能手机的迅猛发展,配置和功能大有追赶桌面计算机的劲头,许多知名浏览器推出的移动平台版也顺势带了手势操作功能,让用户可以通过在屏幕上划出手势快速进行操作,免去了点击菜单的繁琐操作。苹果的设计一直在业内广受好评,一直被模仿,从未被超越。在不断发挥自身设计优势的同时,还借鉴了竞争对手的优良设计,并融入到了自己的产品中。iOS 5的状态栏就是个很好的例子,其下拉设计就借鉴了Android和Windows Phone。这次iOS 5加入手势操作,让使用起来本就非常方便的iOS如虎添翼。
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新}

我要回帖

更多关于 swipe手势 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信