【发布时间】:2011-10-14 23:26:55
【问题描述】:
这个问题太原始了,但我在过去的 8 个小时里一直在尝试,它正在消耗我的精力(以及信心水平:))
在我的课堂上,我已经注册了 Targeted touches。
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
我想检查用户是在执行单击还是双击。在 ccTouchBegan 方法中,首先使用 singleTap 进行触摸,然后使用 doubleTap 进行触摸。我在一个 Cocos2d 论坛中找到了一个非常有趣的解决方案。 (我现在找不到它..)
解决办法是这样的。
switch (touch.tapCount) {
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTouch:) object:singleTouchLocation];
// Handle Double Touch here
break;
case 1:
self.singleTapLocation = ccp(location.x,location.y);
self.singleTouchLocation = touch;
[self performSelector:@selector(handleSingleTouch:) withObject:touch afterDelay:0.3
];
break;
}
一切看起来都运行良好。从本质上讲,您安排了一个延迟的单点触摸处理程序,并查看它是否实际上是双击。如果是双击,则取消单击处理程序并执行双击逻辑。
但我的问题是,在单点触摸处理程序 (handleSingleTouch) 中,我正在向 UI 添加一个 CCSprite。此操作无效。没有添加任何精灵。 (虽然该方法被调用。)。实际上这是可行的,如果我没有延迟地调用选择器,但有延迟,则不会添加精灵。
我不是 Objective C 专家,所以,如果问题过于原始,我深表歉意。
编辑1:原始thread。
编辑 2:发布 handleSingleTouch.. 仅相关代码。
-(void) handleSingleTouch:(UITouch * ) touch {
CGPoint location = [touch locationInView: [touch view]];
CGPoint glLocation = [MainSceneLayer locationFromTouch:touch];
//Single tap
CCLOG(@"Single tap: Adding marker image");
if(zoomedOut) {
CGPoint globalCoordinates = [quadConvertor convertLocalToGlobal:location
inActiveQuadrant:activeQuadrant];
if( [self isTouchValidForSelectedItem:globalCoordinates] == Matched) {
[self addMarkerImages:glLocation];
} else if([self isTouchValidForSelectedItem:globalCoordinates] == NotMatched) {
[missedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
missedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[missedLabel runAction:seq];
} else {
[alreadyAccountedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
alreadyAccountedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible1:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[alreadyAccountedLabel runAction:seq];
}
}
swipeStartPoint = [touch locationInView:touch.view];
}
【问题讨论】:
-
可以发一下handleSingleTouch: 方法
标签: objective-c ios cocos2d-iphone