当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS开发中使用cocos2d添加触摸事件的方法

iOS开发中使用cocos2d添加触摸事件的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

广西北海溺水事故,蘑菇街2013新款冬装,小狒狒音乐天空

cclayer类是用来接收触摸输入的。不过你要首先启用这个功能才可以使用它。你通过设置istouchenabled为yes来让层接收触摸事件:

复制代码 代码如下:
self.istouchenabled = yes;

此项设定最好在init方法中设置。你可以在任何时间将其设置为no或者yes。

一旦启用istouchenabled属性,许多与接收触摸输入相关的方法将会开始被调用。这些事件包括:当新的触摸开始的时候,当手指在触摸屏上移动的时候,还有在用户手指离开屏幕以后。很少会发生触摸事件被取消的情况,所以你可以在大多数情况下忽略它,或者使用cctouchesended方法来处理。

当手指首次触摸到屏幕时调用的方法:

复制代码 代码如下:

-(void) cctouchesbegan:(nsset *)touches withevent:(uievent*)event

手指在屏幕上移动时调用的方法:

复制代码 代码如下:

-(void) cctouchesmoved:(nsset *)touches withevent:(uievent*)event

当手指从屏幕上提起时调用的方法:

复制代码 代码如下:

-(void) cctouchesended:(nsset *)touches withevent:(uievent*)event

当触摸事件被取消时调用的方法:

复制代码 代码如下:

-(void) cctouchescancelled:(nsset *)touches withevent:(uievent*)event

取消事件的情况很少发生,所以在大多数情况下它的行为和触摸结束时相同。

因为触摸事件由cocoa touchapi接收,所以触摸的位置必须被转换为opengl的坐标。

以下是一个用来转换坐标的方法:

复制代码 代码如下:

-(cgpoint) locationfromtouches:(nsset *)touches 

    uitouch *touch = [touches anyobject]; 
    cgpoint touchlocation = [touch locationinview: [touch view]]; 
    return [[ccdirector shareddirector] converttogl:touchlocation]; 

默认情况下,层接收到的事件和苹果uiresponder类接收到的是一样的。cocos2d也支持有针对性的触摸处理。和普通处理的区别是:它每次只接收一次触摸,而uiresponder总是接收到一组触摸。有针对性的触摸事件处理只是简单的把一组触摸事件分离开来,这样就可以根据游戏的需求提供所需的触摸事件。更重要的是,有针对性的处理允许你把某些触摸事件从队列里移除。这样的话,如果触摸发生在屏幕某个指定的区域,你会比较容易识别出来;识别出来以后你就可以把触摸标记为已经处理,并且其它所有的层都不再需要对这个区域再次做检查。

在你的层中添加以下方法可以启用有针对性的触摸事件处理:

复制代码 代码如下:

-(void) registerwithtouchdispatcher 

    [[cctouchdispatcher shareddispatcher] addtargeteddelegate:self priority:kccmenutouchpriority swallowstouches:yes]; 
}

注:如果你把registerwithtouchdispatcher方法留空,你将不会接收到任何触摸事件!如果你想保留此方法,而且使用它的默认处理方式,你必须调用[super registerwithtouchdispatcher]这个方法。


现在,你将使用一套有点不一样的方法来代替默认的触摸输入处理方法。它们几乎完全一样,除了一点:用 (uitouch *)touch 代替 (nsset *)touches 作为方法的第一个参数:

复制代码 代码如下:

-(bool) cctouchbegan:(uitouch *)touch withevent:(uievent *)event {}

-(void) cctouchmoved:(uitouch *)touch withevent:(uievent *)event {}

-(void) cctouchended:(uitouch *)touch withevent:(uievent *)event {}

-(void) cctouchcancelled:(uitouch *)touch withevent:(uievent *)event {}

这里很重要的一点是:cctouchbegan返回的是一个布尔值(bool)。如果你返回了yes,那就意味着你不想让当前的触摸事件传导到其它触摸事件处理器。你实际上是“吞下了”这个触摸事件。

下面来看一个完整的例子:
在自己的layer里面,添加

复制代码 代码如下:

[self setistouchenabled:yes];

以下方法是cocos2d类库的方法:

复制代码 代码如下:

-(void) setistouchenabled:(bool)enabled

{

if( istouchenabled_ != enabled ) {

istouchenabled_ = enabled;

if( isrunning_ ) {

if( enabled )

[self registerwithtouchdispatcher];

else {

ccdirector *director = [ccdirector shareddirector];

[[director touchdispatcher] removedelegate:self];

}

}

}

}

//这句是关键

-(void) registerwithtouchdispatcher

{

ccdirector *director = [ccdirector shareddirector];

[[director touchdispatcher] addstandarddelegate:self priority:0];

}

接下来就实现方法:

复制代码 代码如下:

- (void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event

{

//    for( uitouch *touch in touches ) {

// cgpoint location = [touch locationinview: [touch view]];

//       

// location = [[ccdirector shareddirector] converttogl: location];

// cgpoint touchpos = location;

//       

//        nslog(@"point==%@",nsstringfromcgpoint(touchpos));

// }

   

   

    uitouch* touch = [touches anyobject];

    cgpoint location = [touch locationinview: [touch view]];

    location = [[ccdirector shareddirector] converttogl: location];

    //self.position = location;

     nslog(@"point==%@",nsstringfromcgpoint(location));

}

- (void)cctouchended:(uitouch *)touch withevent:(uievent *)event {

    self.ismoving = false;

}

- (bool)cctouchbegan:(uitouch *)touch withevent:(uievent *)event {

   

    // you can set up a check here if you're not interested in handling every touch.

    // for example if your player is already moving, return no...

   

    bool handletouch = false;

    if (!self.ismoving) {

        handletouch = true;

        self.ismoving = true;

    }

    return handletouch;

   

}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网