【问题标题】:Multiple touch on the iPhone issueiPhone问题上的多点触控
【发布时间】:2012-07-01 16:11:02
【问题描述】:

我怎样才能让用户在玩操纵杆以移动中心的角色时,他们也可以触摸屏幕的右下角(第二次触摸)来开枪?我看过其他问题,但我仍然无法弄清楚......

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

-(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方法?

谢谢

编辑:

我尝试执行第二个选项并执行此操作: 在 view controller.h 中我添加了:

@interface fireView: UIView 
@end 

我在 .m 中添加了:

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

@end 

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

【问题讨论】:

    标签: iphone objective-c touchesbegan


    【解决方案1】:

    使用 2 UIGestureRecognizers。您可以创建 2 个所需大小的不可见视图 - 一个用于操纵杆,一个用于启动按钮。对于每个视图,使用单个手势识别器。然后您将能够通过不同的方法处理这些视图上的点击,而无需检查它是火还是操纵杆。​​p>

    假设您已经有 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: 来处理事件。操纵杆也是如此。

    编辑 第二种选择(建议在 cmets 中) 制作 UIView 的子类,比如

    @interface fireView: UIView
    @interface joystickView: UIView
    

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

    【讨论】:

    • 对于游戏来说,UIGestureRecognizer 引入的延迟确实很麻烦。我会建议你的方法,但要继承两个 UIViews 并使用 touchesBegan
    • 您好,嗯,我尝试执行第二个选项并完成了此操作:在 view controller.h 中我添加了:(at)interface fireView: UIView (at)end 并在 .m 中添加了:( at) 实现 fireView -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"hihihi"); } (at)end 但它没有记录“hihihi”?
    • 您能否将添加 fireView 到游戏主视图的代码以及您认为对问题有用的所有内容作为编辑发布?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多