开发者

Subclass of UIViewController don't catch touch event

开发者 https://www.devze.com 2023-01-18 06:17 出处:网络
I have a parent class (A) that is UIViewController. Than i create class B that is subclass of class A. What happens is that i can\'t catch touch events in class B with methods like to开发者_StackOverf

I have a parent class (A) that is UIViewController. Than i create class B that is subclass of class A. What happens is that i can't catch touch events in class B with methods like to开发者_StackOverflowuchesBegan. But if i implement this methods in class A ... they get called.

@interface A:UIViewController
.....

@interface B:A


To use a UIViewController, must do event like:

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

Hope it help.


You need to subclass UIView to implement the method touchesBegan.

@interface YourCustomView : UIView

@implementation YourCustomView

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

And now set the view of your VC as a "YourCustomView"

@interface YourViewController : UIViewController
{
     YourCustomView*    view  
}


You need to implement those methods in your UIView subclasses, not in UIViewController subclasses.

0

精彩评论

暂无评论...
验证码 换一张
取 消