有两个用class1 "init“方法(底部代码)编写的overLapping按钮,但这两个按钮被添加到(AddChild)另一个类(class2)中。
如果我按这个按钮。这两个按钮可以同时响应两个事件吗?
并且我希望button1响应class1方法
响应class2方法的button2
做这件事最好的方法是什么?
-(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;
}发布于 2012-08-20 23:06:24
如果两个按钮100%重叠,那么你显然不需要两个按钮。只需使用一个按钮调用两个操作,或者使用一个调用另一个方法的操作选择器,或者将两个操作选择器实际分配给一个控件事件。
来自苹果文档中的UIControl -(空)addTarget:(Id)目标操作:(SEL)操作forControlEvents:(UIControlEvents)controlEvents方法:
您可以多次调用此方法,并且可以为>特定事件指定多个目标-操作对。动作消息可以按该顺序可选地包括发送者和事件作为>参数。
如果这两个按钮没有完全重叠,以至于你想要三种行为: button1 (如果触摸button1中不与button2重叠的部分),button2 (如果触摸button2中不与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];
}
}上面的代码没有经过测试,可能也不是最优的,但这只是一个起点,给你一个想法。
https://stackoverflow.com/questions/12009842
复制相似问题