Iphone 我可以让两个重叠的按钮同时响应两个事件吗

Iphone 我可以让两个重叠的按钮同时响应两个事件吗,iphone,ios,ipad,cocos2d-iphone,Iphone,Ios,Ipad,Cocos2d Iphone,在class1“init”方法(底部代码)中编写了两个重叠的按钮,但这两个按钮被添加到(addChild)另一个类(class2)中 如果我按下按钮。这两个按钮是否可以同时响应两个事件 我想让button1响应class1方法 按钮2到响应类2方法 最好的方法是什么 -(id)initWithPosition:(CGPoint)aPoint Mene:(CCMenu *)menu Fun:(SEL)fun { id aTarget = [menu parent]; NSString * im

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

如果我按下按钮。这两个按钮是否可以同时响应两个事件

我想让button1响应class1方法

按钮2到响应类2方法

最好的方法是什么

-(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;
}

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

来自苹果的UIControl-(无效)添加目标:(id)目标操作:(SEL)controlEvents操作:(UIControl事件)controlEvents方法的文档:

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

如果两个按钮没有完全重叠,您需要三种行为:button1(如果触摸按钮n1中不重叠的部分button2)、button2(如果触摸按钮n2中不重叠的部分button1)、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];

    }

}

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

你真的尝试过发生的事情吗?我敢打赌,如果你触摸重叠区域,只有一个按钮可以识别轻触。它可能是最后添加的按钮。