UIViewController的子类不捕获触摸事件

时间:2010-09-29 15:05:05

标签: ios iphone uiviewcontroller

我有一个UIViewController的父类(A)。我创建的B类是A类的子类。发生的事情是我无法使用touchesBegan等方法捕获B类中的触摸事件。但是如果我在A类中实现这些方法......它们就会被调用。

@interface A:UIViewController
.....

@interface B:A

3 个答案:

答案 0 :(得分:7)

要使用UIViewController,必须执行以下事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch;
    CGPoint pos;

    for( touch in touches )
    {
        //pos = [ touch locationInView:self ]; // Only work on UIView
        pos = [touch locationInView:self.view ];       // Work on UIViewController

        //NSLog(@"Touch: %f, %f",pos.x,pos.y);

        // Send X, Y, tapcount
        _faceOff->toucheBegan( pos.x, pos.y, [ [ touches anyObject ] tapCount ]);
    }
}

希望有所帮助。

答案 1 :(得分:4)

你需要继承UIView来实现touchesBegan方法。

@interface YourCustomView : UIView

@implementation YourCustomView

// Override this function to get the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"TOUCH!");
}

现在将VC的视图设置为“YourCustomView”

@interface YourViewController : UIViewController
{
     YourCustomView*    view  
}

答案 2 :(得分:3)

您需要在UIView子类中实现这些方法,而不是在UIViewController子类中。

相关问题