我可以调用 - (void)touchesBegan:(NSSet *)在子视图上触及withEvent:(UIEvent *)事件

时间:2014-02-23 12:58:02

标签: ios objective-c uitouch uiresponder

我认识到我的观点 - 如果发生某些情况,我想用这种方法调用我的子视图,但它不起作用 - (如果我不覆盖hittestWithEvent - 这个视图会被返回)

我该怎么做?

编辑:

我希望原来的SUBVIEW能够获得"点击"所以我试着这样做:

-(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

1 个答案:

答案 0 :(得分:1)

您可以在主视图上覆盖touchesBegan:withEvent,并且条件会在子视图上调用它:

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

希望这就是你所追求的目标。

//扩展

您可以尝试向子视图添加一个方法,该方法在满足条件时调用:

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

并在你的观点中称呼它:

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