Avba
Avba

Reputation: 15266

Can I call - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event on subview

I recognize touches in my view - if certain conditions happen I would like to call my subview with this method but it isn't working - (if I don't override hittestWithEvent - this view would have been returned)

How can I do it?

Edit:

I want the original SUBVIEW to get the "tap" so I tried to do this:

-(void)didTap:(MyTapGesture *)tap
{
    // the original view that would have been returned by hit test. could be any arbitrary view
    UIView *theView = [super hitTest:[tap locationInView:self] withEvent:nil];
    NSSet *touchesBegan = tap.touchesBegan;
    NSSet *touchesEnded = tap.touchesEnded;
    UIEvent *eventBegan = tap.eventBegan;
    UIEvent *eventEnd = tap.eventEnd;

    NSSet *tbegan = [eventBegan touchesForView:theView];
    NSSet *tend = [eventEnd touchesForView:theView];
    // try to simulate the touch in the subview - not working   
    [theView touchesBegan:tbegan withEvent:eventBegan];
    [theView touchesEnded:tend withEvent:eventEnd];
}

@interface MyTapGesture : UITapGestureRecognizer

@property (nonatomic,strong) NSSet *touchesBegan;
@property (nonatomic,strong) NSSet *touchesEnded;
@property (nonatomic,strong) UIEvent *eventBegan;
@property (nonatomic,strong) UIEvent *eventEnd;
@end

@implementation MyTapGesture

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchesBegan = touches;
    self.eventBegan = event;
    [super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchesEnded = touches;
    self.eventEnd = event;
    [super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
}

@end

Upvotes: 2

Views: 3852

Answers (1)

Greg
Greg

Reputation: 25459

You can overwrite touchesBegan:withEvent on your main view and it the condition happen call it on your subview:

//In your view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (CONDITION)
    {
        //call your subview
        [yourSubview touchesBegan:touches withEvent:event];
    }
}

Hope this is what you are after.

//Extended

You can try add a method to your subview which is called when the condition is meet:

//In your subview
-(void)myMethod:(NSSet *)touches
{
    //Your code
}

And call it in your view:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (CONDITION)
    {
        //call your subview
        [yourSubview myMethod:touches];
    }
}

Upvotes: 1

Related Questions