触摸事件
来源:互联网 发布:北京软件测试培训班 编辑:程序博客网 时间:2024/05/17 00:52
、单点触摸事件
单点触摸:
首先进入Layer函数中 81行四个方法重写
void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
//触摸开始,当用户的手指接触到手机屏幕时
void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
//触摸移动,当用户的手指在屏幕上滑动时
void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
//触摸结束,当用户的手指脱离了手机屏幕
void onTouchesCancelled(const std::vector<Touch*>&touches,Event*unused_event);
//触摸取消,当用户的手指还没有脱离手机屏幕,但是出现了手机来电的情况,使我们的游戏进入了后台
bool HelloWorld::onTouchBegan(Touch *touch,Event *unused_event)//opengl
{
Vec2 v=touch->getLocation();//得到当前的触摸点
touch->getPreviousLocation();//得到前一个触摸点的坐标
touch->getStartLocation();//得到第一个点的触摸坐标
log("%f,%f",v.x,v.y);
log("0000");
Rect rect=_sprite->getBoundingBox();
// rect.intersectsRect(const cocos2d::Rect& rect);两个矩形区域是佛相交
if(rect.containsPoint(v))//矩形区域是否包含摸个点
{
return true;
}
return false;//当前对象是否处理
}
//1 获取分发中心
auto d=Director::getInstance()->getEventDispatcher();
//2创建一个单点触摸事件监听器
auto l=EventListenerTouchOneByOne::create();
//l->setSwallowTouches(true);//可以吞噬
l->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
l->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
l->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
l->onTouchCancelled=CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);
//3注册
// d->addEventListenerWithSceneGraphPriority(l, this);
d->addEventListenerWithFixedPriority(l, -1);
_sprite=Sprite::create("CloseNormal.png");
_sprite->setPosition(Vec2(100,100));
this->addChild(_sprite);
15、多点触摸事件
在iOS aAppController.mm58行[eaglView setMultipleTouchEnabled:true];设置允许多点触摸,否则会把多点当成单点
在layer中赋值多点触摸的四个方法
void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
void onTouchesCancelled(const std::vector<Touch*>&touches,Event *unused_event);
//1 获取分发中心
auto d=Director::getInstance()->getEventDispatcher();
//2创建监听器
auto l=EventListenerTouchOneByOne::create();
//l->setSwallowTouches(true);//可以吞噬
l->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
l->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
l->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
l->onTouchCancelled=CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);
//3注册
d->addEventListenerWithSceneGraphPriority(l,this);
Sprite* _sprite=Sprite::create("HelloWorld.pgn");
_sprite->setPosition(Vec2(240,160));
this->addChild(_sprite);
return true;
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
if(touches.size()==2)
{
Touch* touch=touches.at(0);
Vec2 v=touch->getLocation();
Touch* touch2=touches.at(0);
Vec2 v2=touch2->getLocation();
_distance=v.getDistance(v2);//获得两个点之间的距离
}
log("HelloWorld::onTouchesBegan");
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)
{
if (touches.size()==2) {
Touch* touch=touches.at(0);
Vec2 v=touch->getLocation();
Touch* touch2=touches.at(0);
Vec2 v2=touch2->getLocation();
float distance=v.getDistance(v2);
float r=distance/_distance;
_sprite->setScale(_sprite->getScale()*r);
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
}
void HelloWorld::onTouchesCancelled(const std::vector<Touch*>&touches,Event *unused_event)
{
}
多点触摸和单点触摸的区别:
(1)多点默认是不开启的,所以需要先开启一下 (2)多点触摸一定会接受这次的触摸事件,但是单点触摸可以选择是否接受 (3)单点触摸可以有吞噬,多点触摸不能吞噬
(4)单点触摸的优先级永远比多点触摸的高 】
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 触摸事件
- 01背包问题
- Android Studio下NDK的使用
- iOS绘制 - 自定义制作进度条
- Eclipse工具使用技巧总结
- 6.2.6 QMGR_JOB:针对特定邮件和MDA的发送任务
- 触摸事件
- SRoundLayout-逆时针圆形分布Layout
- HTML 5 关于文件读取学习笔记
- hibernate Entity注解
- oracle全文索引
- SPRING-MVC访问静态文件,如jpg,js,css
- 并查集初学——病毒
- 屏幕适配
- 然而沼跃鱼早已看穿了一切——字符串替换