如何使用iOS手势UIGestureRecognizer

 我来答
huanglenzhi
2015-01-23 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
  UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:

  1、拍击UITapGestureRecognizer (任意次数的拍击)
  2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
  3、摇动或者拖拽UIPanGestureRecognizer
  4、滑动UISwipeGestureRecognizer (以任意方向)
  5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
  6、长按UILongPressGestureRecognizer

  对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一 个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码:

  

  一个手指,拍击两次手势
  复制代码
  // 创建一个手势识别器
  UITapGestureRecognizer *oneFingerTwoTaps =
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
  
  // Set required taps and number of touches
  [oneFingerTwoTaps setNumberOfTapsRequired:2];
  [oneFingerTwoTaps setNumberOfTouchesRequired:1];
  
  // Add the gesture to the view
  [[self view] addGestureRecognizer:oneFingerTwoTaps];

  消息方法oneFingerTwoTaps
  - (void)oneFingerTwoTaps
  {
  NSLog(@"Action: One finger, two taps");
  }
  复制代码
  

  两个手指,拍击两次手势
  复制代码
  UITapGestureRecognizer *twoFingersTwoTaps =
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
  [twoFingersTwoTaps setNumberOfTapsRequired:2];
  [twoFingersTwoTaps setNumberOfTouchesRequired:2];
  [[self view] addGestureRecognizer:twoFingersTwoTaps];

  消息方法twoFingersTwoTaps
  - (void)twoFingersTwoTaps {
  NSLog(@"Action: Two fingers, two taps");
  }
  复制代码
  

  一个手指向上、向下滑动手势
  复制代码
  // 向上滑动
  UISwipeGestureRecognizer *oneFingerSwipeUp =
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
  [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
  [[self view] addGestureRecognizer:oneFingerSwipeUp];

  - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
  {
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
  }

  // 向下滑动
  UISwipeGestureRecognizer *oneFingerSwipeDown =
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
  [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
  [[self view] addGestureRecognizer:oneFingerSwipeDown];

  - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
  {
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
  }
  复制代码
  

  旋转手势
  复制代码
  UIRotationGestureRecognizer *twoFingersRotate =
  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
  [[self view] addGestureRecognizer:twoFingersRotate];

  - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
  {
  // Convert the radian value to show the degree of rotation
  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
  }
  复制代码
  

  向里或向外捏的手势
  复制代码
  UIPinchGestureRecognizer *twoFingerPinch =
  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
  [[self view] addGestureRecognizer:twoFingerPinch];

  - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
  {
  NSLog(@"Pinch scale: %f", recognizer.scale);
  }
  复制代码
 
  UITapGestureRecognizer *twoFingersTwoTaps =
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
  [twoFingersTwoTaps setNumberOfTapsRequired:2];
  [twoFingersTwoTaps setNumberOfTouchesRequired:2];
  [[self view] addGestureRecognizer:twoFingersTwoTaps];

  消息方法twoFingersTwoTaps
  - (void)twoFingersTwoTaps {
  NSLog(@"Action: Two fingers, two taps");
  }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
微测检测5.10
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇... 点击进入详情页
本回答由微测检测5.10提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式