1. Touch:在与设备的多点触摸屏交互时生成。
  2.  
  3. 响应者对象
  4. 响应者对象就是可以响应事件并对事件作出处理。在iOS中,存在UIResponder类,它定义了响应者对象的所有方法。UIApplicationUIView等类都继承了UIResponder类这些类的实例都可以当作响应者。
  5.  
  6. 第一响应者
  7. 当前接受触摸的响应者对象被称为第一响应者,即表示当前该对象正在与用户交互,它是响应者链的开端。
  8.  
  9. 响应者链
  10. 事件被交由第一响应者对象处理,如果第一响应者不处理,事件被沿着响应者链向上传递,交给下一个响应者。一般来说,第一响应者是个视图或控件,并且首先对事件进行响应,如果第一响应者不处理该事件,事件就会被传递给它的视图控制器,如果此视图控制器不处理该事件,则将事件传递给父视图,如果父视图没有响应,则该事件转到父视图控制器。
  11. ,以此类推,直到顶层视图。接下来会沿着顶层视图到窗口(UIWindow对象)再到程序(UIApplication对象)。如果UIApplication不响应该事件,该事件逐渐进入睡眠状态。
  12.  
  13. 当发生触摸时,会触发下列方法:
  14. touchesBegan:withEvent
  15. touchesMoved:withEvent
  16. touchesEnded:withEvent
  17. touchesCancelled:withEvent
  18.  
  19. 在给定的触摸阶段中,如果发生新的触摸动作或已有的触摸动作发生变化,应用程序就会发送如下消息:
  20. 当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent:消息。
  21. 当一个或多个手指在屏幕上移动时,发送touchesMoved:withEvent:消息。
  22. 当一个或多个手指离开屏幕时,发送touchesEnded:withEvent:消息。
  23. 当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息。
  24.  
  25. 以上方法都有两个参数。
  26. 第一个参数是一个UITouch对象的集合,表示给定阶段中 新的或者发生变化的触摸动作;第二个参数是一个UIEvent对象,表示这个特定的事件。
  27.  
  28. 为了处理给定阶段的事件,响应者对象常常从传入的集合参数中取得一或多个UITouch对象,然后考察这些对象的属性或取得它们的位置.
  29. .如果需要处理所有触摸对象,可以向该NSSet对象发送anyObject消息。
  30. .UITouch实例的tapCount属性还可以给出发生多少次触碰
  31. .UITouch类中有一个名为locationInView:的重要方法,如果传入self参数值,它会给出触摸动作在响应者坐标系统中的位置
  32.  
  33. 检测双击手势
  34. - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
  35. {
  36. UITouch *touch = [touches anyObject];
  37. if ([touch tapCount] == ) {
  38. CGPoint tapPoint = [theTouch locationInView:self];
  39. // Process a double-tap gesture
  40. }
  41. }
  42.  
  43. 响应者类并不是必须实现上面列出的事件方法。举例来说,如果它只对手指离开屏幕感兴趣,则只需要实现touchesEnded:withEvent:方法就可以了。
  44.  
  45. UILabel *_messageLabel;
  46. @property(nonatomic, retain) UILabel *messageLabel;
  47.  
  48. @synthesize messageLabel = _messageLabel;
  49.  
  50. //创建messageLabel
  51. _messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
  52. _messageLabel.backgroundColor = [UIColor clearColor];
  53. _messageLabel.textAlignment = UITextAlignmentLeft;
  54. [self.view addSubview:messageLabel];
  55.  
  56. - (void)updateMessageLabelFromTouches:(NSSet *)touches touchStr:(NSString *)touchStr
  57. {
  58. int munTaps = [[touches anyobject] tapCount];
  59. NSString *tapsMessage = [NSString stringWithFormat:@"%@,%d touches",touchStr,munTaps]
  60. _messageLabel.text = tapsMessage;
  61. }
  62. // 手指触碰屏幕时
  63. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  64. {
  65. NSString *tmpStr = @"Touches Began";
  66. [sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
  67.  
  68. }
  69.  
  70. // 手指在屏幕上移动时
  71. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  72. {
  73. NSString *tmpStr = @"Touches Moved";
  74. [sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
  75. }
  76.  
  77. // 手指离开屏幕时
  78. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  79. {
  80. NSString *tmpStr = @"Touches Ended";
  81. [sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
  82. }
  83.  
  84. // 当触摸序列被系统事件所取消时
  85. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  86. {
  87. NSString *tmpStr = @"Touches Cancelled";
  88. [sele updateMessageLabelFromTouches:touches touchStr:tmpStr];
  89. }
  90.  
  91. .手势:是指从你用一个或多个手指接触屏幕时开始,直到你的手指离开屏幕为止所发生的所有事件。
  92.  
  93. 2.1.UITapGestureRecognizer敲击手势(单击和双击)
  94.  2.2.UIPanGestureRecognizer(拖动手势)
  95.  2.3.UIPinchGestureRecognizer(缩放手势)
  96.  2.4.UISwipeGestureRecognizer(滑动手势)
  97.  2.5.UIRotationGestureRecognizer(旋转手势)
  98.  2.6.UILongPressGestureRecognizer(长按手势)
  99.  
  100. .UITapGestureRecognizer敲击手势
  101. UIView *_view
  102. _View = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
  103. [_view setBackgroundColor:[UIColor redColor]];
  104.  
  105. // 单击的 Recognizer
  106. UITapGestureRecognizer *tapRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
  107. //点击的次数
  108. tapRecognizer.numberOfTapsRequired = ; // 单击
  109. tapRecognizer.numberOfTapsRequired = ; // 双击
  110. //点击的手指数
  111. singleRecognizer.numberOfTouchesRequired = ;
  112.  
  113. //给view添加一个手势监测;
  114. [tapView addGestureRecognizer:singleRecognizer];
  115.  
  116. // 单击或双击后调用的方法
  117. - (void) tap:(UITapGestureRecognizer *)sender
  118. {
  119. if (sender.numberOfTapsRequired == ) {
  120. [_view setBackgroundColor:[UIColor greenColor]];
  121. }else if(sender.numberOfTapsRequired == ){
  122. //双击
  123. [_view setBackgroundColor:[UIColor yellowColor]];
  124. }
  125. }
  126.  
  127. .1UIPanGestureRecognizer(拖动手势)
  128. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  129. [_view addGestureRecognizer: panRecognizer];
  130.  
  131. -(void) pan:(UIPanGestureRecognizer *) pan
  132. {
  133. [_panView setBackgroundColor:[UIColor yellowColor]];
  134.  
  135. }
  136.  
  137. .1UIPinchGestureRecognizer(缩放手势)
  138. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
  139. [_view addGestureRecognizer: *pinchRecognizer];
  140.  
  141. - (void)pinch:(UIPinchGestureRecognizer *)pinch
  142. {
  143. [_pinchView setBackgroundColor:[UIColor yellowColor]];
  144. }
  145.  
  146. 4.1 UISwipeGestureRecognizer 滑动手势
  147. UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
  148. //设置滑动方向 Down/Left/Right/Up
  149. swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
  150. [_view addGestureRecognizer:swipeRecognizer];
  151.  
  152. - (void)swipe:(UISwipeGestureRecognizer *)swipe
  153. {
  154. [_swipeView setBackgroundColor:[UIColor yellowColor]];
  155. }
  156.  
  157. .1UIRotationGestureRecognizer(旋转手势)
  158. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]
  159. [_view addGestureRecognizer:rotation];
  160.  
  161. - (void) rotation:(UIRotationGestureRecognizer *)rotation
  162. {
  163. [_rotationView setBackgroundColor:[UIColor yellowColor]];
  164. }
  165.  
  166. 6.1 UILongPressGestureRecognizer长按手势
  167. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  168. [_longPressView addGestureRecognizer:longPress];
  169. longPress.minimumPressDuration = 3.0; // 按3秒相应
  170. [_view addGestureRecognizer:longPress];
  171.  
  172. - (void)longPress:(UILongPressGestureRecognizer *)longPress
  173. {
  174. [_longPressView setBackgroundColor:[UIColor yellowColor]];
  175. }

IOS开发之---触摸和手势的更多相关文章

  1. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  2. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...

  3. iOS开发之触摸事件及手势

    1.iOS中的事件 在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: 2.响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...

  4. iOS 开发的几种手势

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...

  5. ios开发之触摸&手势识别

    概要: 4个触摸事件.6个手势识别.响应者链条 1.4个触摸事件 1> 触摸事件主要是针对视图的,包括 - (void)touchesBegan:(NSSet *)touches withEve ...

  6. iOS开发中常用的手势---边缘手势

    说明:以下方法是开发中使用的方法,有什么不对的或者好的方法,请多多指教! 此处的边缘手势是用来控制左侧抽屉视图的弹出以及收回. 添加手势 : 页面上有多个手势时需要遵循 UIGestureRecogn ...

  7. iOS开发UITouch触摸API简介

    1.UITouch简介 当用户触摸屏幕时,会创建一个UITouch对象: UITouch的作用保存着触摸相关的信息,比如触摸的位置.时间.阶段等: 当从开始到结束,系统会更新UITouch对象,结束时 ...

  8. iOS开发:自定义控件实现手势解锁

    自定义控件 1.提供initWithFrame:及initWithCoder:方法来初始化: 2.解锁控件只负责展示.触摸.绘图等,存储轨迹.判断轨迹等操作不是解锁控件要做的.因此要定义一个代理,将轨 ...

  9. IOS开发中长按的手势事件编程

    长按手势事件: 长按按钮1S后改变按钮颜色: // 长按事件 #import "ViewController.h" @interface ViewController (){ UI ...

随机推荐

  1. MSDN中HttpWebRequest/HttpWebResponse用法

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://home.cnblogs.com/u/weiweiboqi/ ...

  2. pyqt QTableWidgetItem多行显示

    def __2(self): t1=QtGui.QTableWidgetItem(self.names.text()) self.tabs.tableinsertinto.setItem(0,0,t1 ...

  3. SmaterWeatherApi---签名加密和数据訪问--简单粗暴一步搞定

    -----------------------------------------------------更新-2014-07-09---------------------------------- ...

  4. OSX: 私人定制Dock默认程序图标

    不论什么一个新用户第一次登陆后,OSX都会自己主动地在用户的Dock中列出系统默认的应用程序图标,这些图标随着OSX版本号的不同而不同. 系统管理员有的时候须要改变这些系统默认图标,或者加入自己的或者 ...

  5. 1. Git 克隆代码

    1. Git 克隆代码 git clone git://github.com/facebook/hiphop-php.git 2. Git更新分支 查看服务器上的所有分支 [huzg@slave3 h ...

  6. Git 版本回退问题详解

    版本回退 git log    ,  git reset --hard xxxx回退到以前的版本 git reflog,  git reset --hard xxx 回退到将来的版本 现在,你已经学会 ...

  7. Js 实现 C# Format方法

    参考网友的, 挺好用的: String.prototype.format = function (args) { if (arguments.length > 0) { var result = ...

  8. c#程序为PDF文件填写表单内容

    众所周知,PDF文件一般情况下是无法修改的,如果你有一张现成的PDF表格,这时想通过编程实现从数据库或者动态生成内容去填写这张表格,就会有些问题了,首先我们要解决以下2个重要的问题: 1.如何将内容写 ...

  9. Kernel Regression from Nando's Deep Learning lecture 5

    require 'torch' require 'gnuplot' , , nData) ) print(xTrain) print(yTrain) local yTrain = yTrain + t ...

  10. UVALive 4119 Always an integer (差分数列,模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Always an integer Time Limit:3000MS     M ...