处理多点触控事件Handling Multi-Touch Events

来源:互联网 发布:10月经济数据全面回落 编辑:程序博客网 时间:2024/04/29 15:20

From:http://www.cocoachina.com/bbs/read.php?tid-4380.html

处理多点触控事件,你自己的定制UIview子类别(或者,不频常,你自己的定制UIApplication或UIwindow子类别),要实行至少在其中的UIResponder方法事件处理。以下章节描述这些方法,讨论的方法处理常用手势,显示出典型的回应者物件处理一个复杂序列多点触控事件,并提出了一些技术事件处理。

在这一章节:

事件的处理方法
处理轻拍的手势
处理重拍的手势
在处理复杂的多点触控顺序
事件处理技巧

事件的处理方法
在一个多点触控序列,应用程式分派了一系列的事件讯息。接受和处理这些信息,回应者物件类别必须实作(implement)至少有下列情形之一的方法(methods)公告

复制代码
  1. UIResponder :
  2. ﹣(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
  3. ﹣(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  4. ﹣(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;



应用程式发送这些讯息,针对某个触摸(touches)阶段,当有新的或改变触摸:

.一个或更多的手指触摸下在萤幕上,它发送了touchesBegan : withEvent :讯息。

.一个或更多的手指移动,它发送了touchesMoved : withEvent :讯息。

.一个或更多的手指从萤幕上离开,它发送了touchesEnded : withEvent :讯息。

每一个这些方法的,是与触摸阶段(例如UITouchPhaseBegan) ,其中任何UItouch物件,你可以找到评估它阶段的属性。

每一个讯息,就是引用一个事件的处理方法,传送两个参数。第一,是一套UItouch物件所代表的新的或经改变触摸,涉及为特定阶段。第二个参数是一个UIEvent物件代表这个特别的事件。从事件物件,你可以得到所有接触物件为事件( allTouches )触摸物件的一个子集,过滤为View或Windows 。一些这些触摸物件代表触及这并没有改变,自上次事件信息的,或已改变,但在不同阶段进行。

回应者物件经常处理的事件,为某一特定阶段所获得一种或一种以上的UItouch物件在传送一套,然后再评估其属性或获得它们的位置。 (如果有任何触摸物件会做,可采取送NSSet物件的一个anyObject信息)的一个重要方法是locationInView :,其中,如果传送self的参数,产生的位置触摸在回应者物件坐标系统(previousLocationInView:) 。一个平行的方法告诉你,上次的位置触摸( previousLocationInView: ) 。 UITouch的属性案例(instance)告诉你有多轻拍(taps)已作出( tapCount ),当触摸己建立或最后己变化(timestamp)且是否触摸被强拍(swipe),如果这样,在哪个方向( info)。
    
        
回应者类别并不实作所有三个这事件的方法上面所列。举例来说,如果它正在寻找一手指时,他们从萤幕离开,它只需实作touchesEnded : withEvent : 。事实上,不是执行上述的任何方法,回应者颣别可实作touchesChangedwithEvent :和监测阶段,触及在那个方法。

    
如果一个回应者建立持久的物件,而在处理事件过程中多点触摸序列,它应实作touchesCanceled处置这些物件时,该系统取消了序列。取消经常发生时,一个外部事件,举例来说,一通电话打乱目前的应用程序的事件处理。注意到一回应者物件个也应处置这些同样的物体,当它接收最touchesEnded : withEvent :留言,为一个多点触摸序列。 (见"事件处理技术" Event-Handling Techniques),以了解如何确定最后的触摸在一个序列) 。

处理轻拍的手势

一件很平常的手势,在iPhone的应用程式中是轻拍(tap):使用者他或她的手指轻拍(taps)一个物件。回应者物件能回应一个单一的轻拍中的一个方法,一个双轻拍(double-tap)在另一个,并可能分三轻拍(tripe-tap),但另一种方式表达。以确定有多少次使用者轻拍回应者物件,你会得到tapCount属性值的一个UITouch物件。

最好的地方找到这个值(Value)所采用的方法touchesBegan : withEvent :和touchesEnded : withEvent :。在很多情况下,后者的方法是首选,因为它对的回应至于触摸阶段在使用者轻离开拍。由寻找该轻拍的计算(count)轻拍后往上(touch-up)阶段( UITouchPhaseEnded ) ,你确保手指,实在是打字中,比如,在案例,轻拍下(touching down)然后拖曳(dragging )。

    
在列表8-1 , touchesEnded : withEvent :方法实作回应双轻拍(double-tap)手势缩放的内容显示在一个scroll View。

    
列表8-1处理双双轻拍(double-tap)手势

复制代码
  1. - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
  2. {
  3.      UIScrollView *scrollView = (UIScrollView*)[self superview];
  4.      UITouch *touch = [touches anyObject];
  5.      CGSize size;
  6.      CGPoint point;
  7.      if([touch tapCount] == 2) {
  8.          if(![_viewController _isZoomed]) {
  9.              point = [touch locationInView:self];
  10.              size = [self bounds].size;
  11.              point.x /= size.width;
  12.              point.y /= size.height;
  13.              [_viewController _setZoomed:YES];
  14.              size = [scrollView contentSize];
  15.              point.x *= size.width;
  16.              point.y *= size.height;
  17.              size = [scrollView bounds].size;
  18.              point.x -= size.width / 2;
  19.              point.y -= size.height / 2;
  20.              [scrollView setContentOffset:point animated:NO];
  21.          }
  22.          else
  23.          [_viewController _setZoomed:NO];
  24.      }
  25. }



一个复杂出现时,回应者物件要处理的单轻拍和双轻手势有不同的方式。举例来说,一个单轻拍可能会选择物件和一个双轻拍可能会显示View为编辑该项目这就是双轻拍。又是怎样回应者物件知道,就是单轻拍,是不是第一次的一个组成部分双轻里是如何回应者物件可以处理这种情况使用事件处理方法只是描述:

1.
在touchesEnded : withEvent :当自轻拍计数是一个,该回应者物件发送itself本身是一个performSelector : withObject : afterDelay :讯息。在选择确定了另一种方法实作,由回应者处理单轻拍手势;物件为第二个参数是相关UITouch物件;延误(delay)是了一些合理时间间隔去区分单轻拍和双轻手势。

2.
在touchesBegan : withEvent :,如果轻拍计数是两个,回应者物件对取消悬而未决的延迟履行发送本身cancelPreviousPerformRequestsWithtarget :消息。如果轻拍计数,是不是两个,该方法所确定的选择在1.为单轻拍手势是引用后拖延。

3.
在touchesEnded : withEvent : ,如果轻拍计数是两个是两个,回应者履行必要行动,处理双轻手势。

处理重拍的手势

当使用者非常迅速的一个或一个以上的手指穿过萤幕时,系统认为这是重拍的手势。重拍手势常常被用来滚动的内容或去向前和向后的一个相连的一系列项目。该系统监视涉及在屏幕上动作,如果用手指的方向和速度都是一致可能是重拍手势,它合集info属性的相关UITouch物件;属性表明不仅发生一重拍,但其方向。由于这种检测需要跟踪的触摸随着时间的推移,行踪,可在一个UITouchPhaseMoved阶段,并没有被标示为一重拍,即使后来变成了有标记的,因为重拍。

注:一重拍手势,是编程等于一快速轻拍手势,虽然他们有不同的人机界面的内涵。还能用,快速轻拍是使用转移页面之间的内容,如照片。一重拍,是用来改变一些元素;举例说,重拍在一封电子邮件上,造成删除按钮出现。

但是,该系统可能稍后不标注触摸作为重拍,如果随后跟踪的手指显示了运动不一致一重拍的手势。如果一个回应者物件在寻求一个重拍在一个特定方向,并没有理会,最后重拍触摸的状态,它可以测试info属性的任何一项UIResponder事件的处理方法。一般来说,你最有兴趣,是否触动作是一重拍,在触摸物件的UITouchPhaseEnded阶段。你会因此寻找和评估,重拍常数在touchesEnded : withEvent :方法。

    
列表8-2显示一个简单touchesEnded : withEvent :实作是为了寻找重拍(swipes)至左和右,并执行适当的行动方向。

列表8-2处理重拍的手势

复制代码
  1. -- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  2.      UITouch *touch = [touches anyObject];
  3.      if (touch.info & UITouchInfoSwipedRight) {
  4.          [self showPreviousImage];
  5.      } else if (touch.info & UITouchInfoSwipedLeft) {
  6.          [self showNextImage];
  7.      }
  8.      return;
  9. }



该方法取得了一个触摸物件从通过在设置评估bit mark物件的info属性,以确定是否UITouchInfoSwipedRight和UITouchInfoSwipedLeft是常量。注意,当该系统探测到重拍的手势,在一个方向,如"向右上角的萤幕上" ,它规定了两个常数bit mark比,以显示这个方向(例如, UITouchInfoSwipedRight和UITouchInfoSwipedUp ) 。

在处理复杂的多点触控顺序

轻拍(taps)及重拍(swipes)是简单的手势。处理多点触控摸顺序是更为复杂-效果上,诠释一个特定应用手势取决于哪些应用程式正试图完成的任务。你可能要跟踪所有涉触模的阶段,录制触摸属性已改变,变更内部状态的。

最好的方法传达你如何可能处理复杂的多点触控摸顺序,用一个例子。列表8-3日,显示出如何定制UIView物件回应触及动态的"欢迎"的标语牌围绕在萤幕上当用手指的动作,当用户重拍的手势,并改变欢迎的语言的。

复制代码
  1. (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3.      UITouch *touch = [[event allTouches] anyObject];
  4.      // Only move the placard view if the touch was in the placard view
  5.      if ([touch view] != placardView) {
  6.          // On double tap outside placard view, update placard's display string
  7.          if ([touch tapCount] == 2) {
  8.              [placardView setupNextDisplayString];
  9.          }
  10.          return;
  11.      }
  12.      // "Pulse" the placard view by scaling up then down
  13.      // Use UIView's built-in animation
  14.      [UIView beginAnimations:nil context:NULL];
  15.      [UIView setAnimationDuration:0.5];
  16.      CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
  17.      placardView.transform = transform;
  18.      [UIView commitAnimations];
  19.      [UIView beginAnimations:nil context:NULL];
  20.      [UIView setAnimationDuration:0.5];
  21.      transform = CGAffineTransformMakeScale(1.1, 1.1);
  22.      placardView.transform = transform;
  23.      [UIView commitAnimations];
  24.      // Move the placardView to under the touch
  25.      [UIView beginAnimations:nil context:NULL];
  26.      [UIView setAnimationDuration:0.25];
  27.      placardView.center = [self convertPoint:[touch locationInView:self] fromView:placardView];
  28.      [UIView commitAnimations];
  29. }
  30. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  31. {
  32.      UITouch *touch = [[event allTouches] anyObject];
  33.      // If the touch was in the placardView, move the placardView to its location
  34.      if ([touch view] == placardView) {
  35.          CGPoint location = [touch locationInView:self];
  36.          location = [self convertPoint:location fromView:placardView];
  37.          placardView.center = location;
  38.          return;
  39.      }
  40. }
  41. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  42. {
  43.      UITouch *touch = [[event allTouches] anyObject];
  44.      // If the touch was in the placardView, bounce it back to the center
  45.      if ([touch view] == placardView) {
  46.          // Disable user interaction so subsequent touches don't interfere with animation
  47.          self.userInteractionEnabled = NO;
  48.          [self animatePlacardViewToCenter];
  49.          return;
  50.      }
  51. }



注:定制客户化View,重新划定自己在回响应事件的处理,一般只应制定一套状态在事件的处理方法,并履行所有的绘画在drawrect :方法。要了解更多关于提请查看内容,参见" Graphics and Drawing. " 。

程式码则表8-4说明了两种不同的技术。首先,它处理所有接触阶段,在touchesChangedWithEvent :方法,取代由个人接触阶段方法。其次,它处理多点接触,利用每一个接触去演奏"prong" ,在模拟乐器( a kalimba )

列表8-4处理多点触控在touchesChangedWithEvent:

复制代码
  1. - (void)touchesChangedWithEvent:(UIEvent *)event
  2. {
  3.      // if the instrument body is showing...
  4.      if (self.frontShowing) {
  5.          //iterate through all the touches currently active
  6.          NSSet *touches = [event allTouches];
  7.          for (UITouch *myTouch in touches) {
  8.          CGPoint location = [myTouch locationInView:self];
  9.          //if it's a new touch
  10.              if (myTouch.phase == UITouchPhaseBegan) {
  11.                  //test for all kalimba prongs
  12.                  NSInteger i;
  13.                  for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
  14.                      if (CGRectContainsPoint(hitRectPlays, location)) {
  15.                          //got one... play the note and make it glow
  16.                          [[InstrumentModel sharedInstrumentModel] playNoteForRegion:(i)];
  17.                          [[self.glowLayers objectAtIndex:i] setHidden: NO];
  18.                      }
  19.                  }
  20.              } else if (myTouch.phase == UITouchPhaseMoved) {
  21.              CGPoint oldLocation = [myTouch previousLocationInView:self];
  22.              // if touch moved off a prong, stop glowing
  23.              NSInteger i;
  24.              for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
  25.                  if (CGRectContainsPoint(hitRectPlays, oldLocation) &&
  26.                        !CGRectContainsPoint(hitRectPlays, location)) {
  27.                      [[self.glowLayers objectAtIndex:i] setHidden: YES];
  28.                      }
  29.                  }
  30.              } else if (myTouch.phase == UITouchPhaseEnded) {
  31.              // if touch ends on a prong, stop glowing
  32.              NSInteger i;
  33.              for (i = 0; i < NUMBER_OF_PRONGS ; i++) {
  34.                  if (CGRectContainsPoint(hitRectPlays, location)) {
  35.                      [[self.glowLayers objectAtIndex:i] setHidden: YES];
  36.                      }
  37.                  }
  38.              }
  39.          }
  40.      }
  41. }



注:程式码用在列表8-3,来自moveme的sample,你可以深入研究,以获得更深入的了解事件的处理情况。

事件处理技术
    
这里有一些事件的处理技巧,你可以用在你的程式中。
    
.跟踪UITouch物件的变化

在你的事件处理程式码,你可以储存有关接触位元(bit)状态,为后来的比较与变化UITouch案例(instance)。作为一个例子,说你要比较的最后位置,每一个接触到它原来的位置。在touchesBegan : withEvent :方法,你还可以得到原来的位置,每一个触摸,从locationInView属性和储存那些在CFDictionary物件使用地址的uitouch物件作为key。其后,在touchesended : withevent使用方法:你可以使用的地址,每个通过在uitouch物件,以获取该物件的原始位置,并比较其目前的位置。 (你应该使用cfdictionary物件,而不是一个nsdictionary物件;后者的复制品,其key,但uitouch颣别并不采用nscopying议定书,其中规定,为物件拷贝) 。

. Hit-testing就触摸在subview或Layer
一个客制化View的可以用hitTest : withEvent :方法UIView或hitTest :方法CALayer找到subview或层就是接受一个触摸和妥善的处理处理事件。下面的例子侦测时, "info"的image在客户化View我一层,是轻拍。

复制代码
  1. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  2.      CGPoint location = [[touches anyObject] locationInView:self];
  3.      CALayer *hitLayer = [[self layer] hitTest:[self convertPoint:location fromView:nil]];
  4.      if (hitLayer == infoImage) {
  5.          [self displayInfo];
  6.      }
  7. }



如果你有一个客制的View与subviews ,你需要来决定是否需要处理涉及在subview等级或superview等级。如果subviews不办处理实作touchesBegan : withEvent : , touchesEnded : withEvent : , touchesMoved : withEvent :,或touchesChangedWithEvent : ,然后这些讯息传达了回应者链向superview 。但是,由于多种轻拍及多重触及正相关,与subviews那里,他们第一次发生, superview将不会接受这些触及。为确保接待,对各种触及, superview应凌驾hitTest : withEvent :要恢复本身,而不是它的任何subviews 。

.决定当最后手指在一个多点摸序列已解除
当你想知道当最后手指在一个多点触摸顺序是否己离开View,比较有多少UITouch物件在传送一套与若干触模为了View所维系传送在UIEvent物件。例如:

复制代码

  1. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
  2.      if ([touches count] == [[event touchesForView:self] count]) {
  3.          // last finger has lifted....
  4.      }
  5. }
原创粉丝点击