gpt4 book ai didi

iOS开发之手势gesture详解

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章iOS开发之手势gesture详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言   。

在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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
_pangesturerecognizer = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlerpangesture:)];
  _pangesturerecognizer.delegate = self;
  _pangesturerecognizer.maximumnumberoftouches = 2;
  _pangesturerecognizer.minimumnumberoftouches = 2;
  [self.view addgesturerecognizer:_pangesturerecognizer];
 
- ( void )handlerpangesture:(uipangesturerecognizer *)recognizer
{
  if ((recognizer.state == uigesturerecognizerstatebegan) ||
   (recognizer.state == uigesturerecognizerstatechanged))
  {
   cgpoint offset = [recognizer translationinview:self.view];
   cgrect frame = self.rightviewcontroller.view.frame;
   frame.origin.x += offset.x;
   if (frame.origin.x >= 0 && frame.origin.x <= kscreenwidth)
   {
    self.rightviewcontroller.view.frame = 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
2
3
4
5
6
7
8
9
10
11
12
13
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失败以后接受者才发生,否则从不会发生.

?
1
[self.panrecognizer requiregesturerecognizertofail:self.swiperecognizer];

允许2个手势同时操作 。

?
1
gesturerecognizer:shouldrecognizesimultaneouslywithgesturerecognizer:

禁止在某一点发生gesture recognizers 。

?
1
2
3
4
5
6
7
8
9
- ( bool )gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldreceivetouch:(uitouch *)touch
{
  if ([touch.view iskindofclass:[uicontrol class ]])
  {
   return no;
  }
 
  return yes;
}

指定一个单向关系两个手势识别器 。

想控制两个识别器相互作用,但你需要指定一个单向关系,您可以重写或canpreventgesturerecognizer:或canbepreventedbygesturerecognizer:子类方法。return yes。例如,如果你想要一个旋转的姿态来防止捏动作,但你不想夹手势防止旋转的姿态。例如,你想一个旋转手势阻止一个缩放手势,但你不想一个缩放手势阻止旋转手势,就加入下面代码 。

?
1
[rotationgesturerecognizer canpreventgesturerecognizer:pinchgesturerecognizer];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html 。

最后此篇关于iOS开发之手势gesture详解的文章就讲到这里了,如果你想了解更多关于iOS开发之手势gesture详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com