【问题标题】:Can I let two overLapping buttons response to two events same time我可以让两个重叠按钮同时响应两个事件吗
【发布时间】:2012-08-17 16:47:41
【问题描述】:

class1“init”方法中写了两个overLapping按钮(底部代码),但是这两个按钮被添加到(addChild)另一个类(class2)

如果我按下按钮(按钮)。两个按钮可以同时响应两个事件吗?

我想让 button1 响应 class1 方法

button2 响应 class2 方法

最好的方法是什么?

-(id)initWithPosition:(CGPoint)aPoint Mene:(CCMenu *)menu Fun:(SEL)fun
{

id aTarget = [menu parent];

NSString * imageName = @"light.png";


CCSprite* sprite1 = [CCSprite spriteWithSpriteFrameName:imageName];
CCSprite* sprite2 = [CCSprite spriteWithSpriteFrameName:imageName];
self.flashitemSprite=[CCMenuItemSprite itemWithNormalSprite:sprite1 selectedSprite:sprite2 target:aTarget selector:fun];
sprite1.visible=YES;
_flashitemSprite.position  = _flashSprite.position;
[menu addChild:self.flashitemSprite];


CCSprite* sprite3 = [CCSprite spriteWithSpriteFrameName:imageName];
CCSprite* sprite4 = [CCSprite spriteWithSpriteFrameName:imageName];
self.aItemSprite =[CCMenuItemSprite itemWithNormalSprite:sprite3 selectedSprite:sprite4 target:self selector:@selector(distroy)];
sprite3.visible=YES;
_aItemSprite.position  = _flashSprite.position;
[menu addChild:self.aItemSprite];


return self;
}

【问题讨论】:

  • 你真的试过会发生什么吗?我敢打赌,如果您触摸重叠区域,则只有一个按钮会识别点击。它可能是最后添加的按钮。

标签: iphone ios ipad cocos2d-iphone


【解决方案1】:

如果两个按钮 100% 重叠且大小相同,那么您显然不需要两个按钮。只需一个按钮调用两个动作,或者通过使用一个调用另一种方法的动作选择器,或者通过将两个动作选择器实际分配给一个控件事件。

来自 Apple 的 UIControl 文档 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 方法:

您可以多次调用此方法,并且您可以为特定事件指定多个目标-动作对。操作消息可以选择包含发送者和事件作为>参数,按该顺序。

如果两个按钮没有完全重叠,那么您需要三种行为:按钮 1(如果触摸按钮 1 的不重叠按钮 2 的部分),按钮 2(如果触摸按钮 2 的不重叠按钮 1 的部分) , button1 & button2 (如果触摸重叠部分)基于用户触摸的位置。 . .

那么您应该在最顶部的按钮中有代码来测试触摸是否在另一个按钮内。像这样的:

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
    UIView *button = (UIView *)sender;
    UITouch *touch = [[event touchesForView:button] anyObject];
    CGPoint location = [touch locationInView:button];
    CGPoint otherButtonLocation = [location locationInView:otherButton];

    if ([otherButton pointInside:otherButtonLocation withEvent:nil]) {

         [self otherButtonAction:otherButton];

    }

}

上面的代码没有经过测试,可能不是最优的,只是一个起点,可以给你一个想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多