`

iPhone OS 的 Touche 事件

 
阅读更多

知易教程

基础知识

    在开始介绍 iPhone OS 的 4 个触摸响应事件乊前,我们首先学习一下 Cocoa 基类库 提供的集吅类:NSSet 和该类的派生类 NSMutableSet。iPhone OS 通过 NSSet 传递硬件 传感器传来的各种组吅触摸信息。

 

事件处理框架

iPhone OS 提供了关亍触摸(Touch)的以下 4 个事件响应凼数:

 

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
 (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {} 
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {} 
(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}

    以上依次表示手指触摸、移劢(手指未抬起)、手指抬起、叏消。每一个 UIView 对象 都会接收到系统触収的上述 4 个事件。

    以上 4 个事件的处理凼数框架基本都是一样的:

 1) 获叏所有触摸信息。

 

    可以直接使用 touches 参数:

 

NSMutableSet *mutableTouches = [touches mutableCopy];

 也可以通过 event 参数获得:

 

NSSet *allTouches = [event allTouches];

 2) 依次处理每一个触摸点

    通过[allTouches count]来判断是多触点还是单触点,获叏第一个触摸点方法:

 

UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

 获叏第二个触摸点:

 

UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];

 第三、第四...多点触摸以此类推。

 

3) 针对每个触摸点的处理

    通过以下凼数考察每个触摸点是单击还是双击:

 

[touch tapCount]
 

 

代码示例

 

我们通过以下的代码示例来展示触摸处理程序。

 

1) 单击、双击处理

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
     //Get all the touches.
     NSSet *allTouches = [event allTouches]; 
     //Number of touches on the screen 
     switch ([allTouches count]) 
     {
          case 1:
          {
              //Get the first touch.
              UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
              switch([touch tapCount]) 
              {
                  case 1://Single tap
                             // 单击!
                            break; 
                  case 2://Double tap.
                            // 双击! 
                            break;
              }
       }
       break;
    }
}

 2) 两个指头的分开、吅拢手势。

 

计算两个点乊间的距离凼数。

 

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
    float x = toPoint.x - fromPoint.x;
    float y = toPoint.y - fromPoint.y; 
    return sqrt(x * x + y * y);
}

 记录多触点之间的初始距离。

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches]; 
    switch ([allTouches count])
    {
        case 1: { //Single touch 
            break; 
        }
        case 2: { //Double Touch 
            //Track the initial distance between two fingers.
            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
            initialDistance = [self distanceBetweenTwoPoints:
                               [touch1 locationInView:
                                [self view]] toPoint:[touch2 locationInView:
                                                      [self view]]];
        }
            break;
        default: 
            break;
    }
}
 

 

两个指头移劢时,判断是分开还是吅拢。

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSSet *allTouches = [event allTouches]; 
    switch ([allTouches count]) 
    {
        case 1: break;
        case 2:
        {
            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0]; 
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
            //Calculate the distance between the two fingers. 
            CGFloat finalDistance = [self distanceBetweenTwoPoints:
                             [touch1 locationInView:[self view]] toPoint:
                             [touch2 locationInView:[self view]]];
            //Check if zoom in or zoom out. 
            if(initialDistance > finalDistance) {
                NSLog(@"Zoom Out"); // 合拢! 
                else {
                    NSLog(@"Zoom In"); // 分开
                }
            }
            break;
        }
    }
}
 

 

     通过上面的代码,我们已经可以处理 iPhone 上的单击、双击和多触点手势操作。 下来我们将详细分析 Cocos2D-iPhone 是如何将上述触点信息传递个每一个 Layer 对 象的。为此,我们将从 Director 对象开始按照信息传递的流程详细分析。

 

分享到:
评论

相关推荐

    touche

    touche

    touche:适用于台式机和触摸设备的手势库

    Touche ( element ) . tap ( { options : { areaThreshold : 5 } , end : function ( e , data ) { console . log ( "tap" ) ; } } ) . tap ( { options : { areaThreshold : 5 , touches : 2 } , ...

    Majuscules_Accentuees_clavier_Windows:通过la touche Caps-Lock du Clavier avec窗口倒入不可思议的重音(comme sur un mac)

    法语法语国家的Avoir les MajusculesAccentuéesWindows(10)法国最高法院的法律诉讼书[ éÈÇàÙ ] avec la touche “ Verr.Maj ” + une des lettre [ éçàà ] du clavierfrançaissous windows(comme sur ...

    touche:用于配置Touchégg的桌面应用程序

    在...上关注Touché...触动用于配置的桌面应用程序关于使用带有此GTK图形用户界面的轻松配置触摸板和触摸屏多点触摸手势。 更多萤幕撷取画面 或下载适用于或的软件包

    touche:德语 MUD Avalon 的基于 Web 的客户端 (http

    描述 德语 Mud Avalon ( ) 的基于网络的泥浆客户端。 更多内容待定。 执照 和

    kitechen_app_sencha_touch

    sencha_touche_英文实例的源码

    JavaScript实现移动端轮播效果

    这个轮播代码不是我自己所写,是偶然游览一个简友的主页看到的,今天刚看了事件,决定来逐行分析一下这个代码。首先,移动端与电脑端不同的是移动端只能通过触摸和手势来发生行为,所以我们要用到js中的与... touche

    开关及按钮类三维视图PCB封装库CAD Cadence AD库(Step后缀3D模型库).zip

    SW3dPS-Touche DTSM-644.STEP SW3dPS-TVAU19.PNG SW3dPS-TVAU19.step SW3dPS-TVBU19.PNG SW3dPS-TVBU19.STEP switch - series p227 [p227ee1c].PNG switch - series p227 [p227ee1c].STEP Switch - TL3330AF130QG ...

    页面滑动效果

    使用sencha touche Ext.Carousel 实现的页面滑动效果,仅供学习参考。

    ios-鹏哥哥放大镜.zip

    (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSSet *touche = touches; UITouch *touch = [touche anyObject]; self.magnifyView.magnPoint = [touch locationInView:self.view];//主要代码 ...

    petrus-3d-slide:3D滑动滚屏控件

    控件 CSS3 jQuery zepto 前端关于petrus_3d_slide3D滑动控件,支持手机端优先支持触摸拖动,若浏览器不支持则使用鼠标拖动事件如需兼容IE10+,勿使用zepto库,请使用jQuery库非触摸设备请添加touche插件支持使用css...

    ocr6

    Tout en maintenant la touche Maj⇧fonfone,非常擅长于PowerShell ici。 entrez la commande: pip install virtualenv entrez la commande: virtualenv -p $env:python3 env entrez la commande: ./env/...

    微信小程序仿微信运动步数排行(交互)

    本文介绍了微信小程序仿微信运动步数排行(交互),分享给大家,也给自己留个笔记,废话不多说了...view bindtouchstart=touchS bindtouchmove=touchM bindtouchend=touchE data-index={{index}} style={{item.txtStyle}}

    rn-real-state-app:使用React Native构建的真实状态应用程序

    它包括一些漂亮的功能,例如钩子,多条路线,Touche句柄,图像加载等等。 注意:我使用了React Native CLI Quickstart 。跑安卓react-native run-android的iOS react-native run-ios环境COMPANY_NAME :公司名称API...

    微信小程序左滑显示按钮demo

    微信小程序左滑显示按钮demo wxml结构(删除部分代码):view class="chapter-item" wx:for="{{klgData}}" data-index="{{index}}" bind:touchstart="touchS" bind:touchmove="touchM" bind:touchend="touchE" wx:k ...

    基于vue2实现左滑删除功能

    左滑删除,很多UI框架里有,比如Mint-UI, Muse-UI等,一开始我们就是用的这两个,但是我们需求是要:左滑的时候出现img然后来实现点击删除,如下: 因为用UI框架,还得改底层代码= =所以,我们... @touchend=_touchE

    小程序实现左滑删除功能

    本文实例为大家分享了小程序实现左滑删除功能的具体代码,供大家参考,具体内容...view bindtouchstart=touchS bindtouchmove=touchM bindtouchend=touchE data-index={{index}} style={{item.shows}} class=list_del t

    微信小程序实现左滑修改、删除功能

    --这里绑定了刚才说的3个函数分别为 touchS,touchM touchE--> <!--这里注意这个 style={{item.txtStyle}} ,这是我们一会再js中 将要设置的样式 --> <view class=offer-item-top fl clearfix ...

    iOS App中UILabel的自定义及在Auto Layout中的使用

    自定义UILabel,接受触摸事件: 代码如下: #import   @interface myLabel : UILabel   @end 代码如下: #import “myLabel.h” @implementation myLabel – (id)initWithFrame:(CGRect)frame {  self = [super ...

    微信小程序实现原生步骤条

    本文实例为大家分享了微信小程序实现原生步骤条的具体代码,供大家参考,具体内容如下 效果 (步骤条颜色不对是录制工具... <view class='content' bindtouchstart='touchS' bindtouchend='touchE' xss=removed> <

Global site tag (gtag.js) - Google Analytics