質問

I have a view (parent) with two subviews, one on top (topChild) of the other (bottomChild).

If I tap on the screen only topChild and parent receive the touch event.

What should I change to propagate the touch event to bottomChild as well?

The code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    MYView* parent = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    parent.tag = 3;
    parent.backgroundColor = [UIColor redColor];

    MYView* bottomChild = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
    bottomChild.tag = 2;
    bottomChild.backgroundColor = [UIColor blueColor];
    [parent addSubview:bottomChild];

    MYView* topChild = [[MYView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    topChild.tag = 1;
    topChild.backgroundColor = [UIColor greenColor];
    [parent addSubview:topChild];
    [self.view addSubview:parent];
}

Where MYView is a subclass of UIView that only logs touchesBegan.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%d", self.tag);
    [super touchesBegan:touches withEvent:event];
}

The result:

enter image description here

Touching the green area produces the following log:

TouchTest[25062:f803] 1
TouchTest[25062:f803] 3

My first idea was to make parent propagate all the touchesSomething calls to its children but (A) I suspect there might be an easier solution and (B) I don't know which child sent the event to parent, and sending touchesSomething messages twice to the same view might cause shenanigans.

After asking the question I've found this post that suggests to override hitTest to change the view that receives the touches. I will try this approach and update the question if it works.

役に立ちましたか?

解決

It's an interesting problem you have that is probably something best addressed through a rethink of the way you have things structured. But to make it work the way you suggest you need to catch the touch event in the current top view, pass it to the parent and then propagate it down through all subviews of the parent view. To make this work you would need the touchesBegan: (or whatever other method you use to intercept the touch) to do nothing in all the views, doing the action only in the method called by the parent view.

Which is really another way of saying don't handle touches in the views, catch them but notify the parent view view and then call subview methods as required to cause the effect you want.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // Do nothing, parent view calls my parentNotifiedTouchesBegan method
    [self.superview touchesBegan:touches withEvent:event];
}

- (void) parentNotifiedTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    // Act on the touch here just as my sibling views are doing
}

Note I changed super to self.superview in that code. You may or may not also want to call super's method depending on what you are doing, and the place to call that may be in parentNotifiedTouchesBegan.

You can know which subView sent the event of course, just use a custom method to notify the superview instead of calling its touchesBegan:. Use a self argument.

他のヒント

If you don't need the touches on the children then set

bottomChild.userInteractionEnabled = NO;
topChild.userInteractionEnabled = NO;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top