2012-07-01 54 views
0

我该如何做到这一点,以便当用户在操纵杆上玩中移动角色时,他们也可以触摸屏幕的右下角(第二次触摸)来开枪?我已经看了看其他的问题,但我仍然无法弄清楚......iPhone上的多点触控问题

这是基本的代码....:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
//make the touch point... 
UITouch *touch = [[event allTouches]anyObject]; 
CGPoint point = [touch locationInView:touch.view]; 

if (//touching a certain area (on the joystick)) { 
//do stuff 
} 

else if (point.x > 300 && point.y > 200) { 
/fire method 

} 




} 

所以基本上我怎么叫的touchesBegan再次找到位置CGPoint要点,如果应该调用fire方法,就研究一下吧?

感谢

编辑:

我试图做的是第二个选项,并做到了这一点:我添加 鉴于或者Controller.h:

@interface fireView: UIView 
@end 

在.M我说:

@implementation fireView -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {   
NSLog(@"hi"); 
} 

@end 

....但它不记录/打印“嗨”?

回答

0

使用2 UIGestureRecognizers。您可以创建2个需要尺寸的不可见视图 - 一个用于操纵杆,一个用于开火按钮。对于每个视图使用单个手势识别器。然后,您将能够通过不同的方法处理这些视图上的水龙头,而无需检查它是否是火或摇杆。

让我们认为你有2个视图 - joystickView和fireView已经。然后像

UITapGestureRecognizer* fireTapGestureRec= [[UITapGestureRecognizer alloc] 
          initWithTarget:self action:@selector(fireTapped:)]; 
    fireTapGestureRec.delegate = self; 
    fireTapGestureRec.numberOfTapsRequired = 1; 
    fireTapGestureRec.numberOfTouchesRequired = 1; 
    [fireView addGestureRecognizer:fireTapGestureRec]; 
    [fireTapGestureRec release]; 

和写fireTapped:来处理事件。操纵杆也是一样。

编辑 第二个选项(在评论建议) 制作的UIView的子类,像

@interface fireView: UIView 
@interface joystickView: UIView 

并为每个子类写自己的touchesBegan:touchesEnded:

+0

我如何使用它? – Bob

+0

对于游戏而言,UIGestureRecognizer引入的滞后真的很麻烦。我会建议你的方法,但要继承两个UIViews并使用touchesBegan – Mazyod

+0

是的。这样更好。 –