Passing events to another view

I have a situation where I would like to place a UIView (this is not my code) on top of another UIView and have events that propagate at the bottom of the screen. To be more specific, I would like to place a UIView on top of MKMapView.

I have a UIView that contains both views (like subviews). I would like my uiview to pass events from my view to both of its views.

.h:

@interface MyUIView : UIView

@end

.m:

#import "MyMapView.h"

#import <MapKit/MapKit.h>

#import "MapGestureRecognizer.h"

@implementation MyMapView

MKMapView *mkMapView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    mkMapView = [[MKMapView alloc] initWithFrame:frame];
    [self addSubview:mkMapView];

// Create another view to place on top of the mkMapView
// This is not my view I do not control is codebase.  I would like to put it on top of the mkMapView
// and still have the mkMapView receive touch events
    UIView * otherView = [[UIView alloc] initWithFrame:frame];
    otherView.backgroundColor = [UIColor clearColor];
    [self addSubview:otherView];

    MapGestureRecognizer *mgr = [[MapGestureRecognizer alloc] init];
    mgr.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
        UITouch *touch = [[event allTouches] anyObject];
        NSLog(@"touches began=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

        [mkMapView touchesBegan:touches withEvent:event];

    };

    mgr.touchesCancelledCallback = ^(NSSet * touches, UIEvent * event) {
        UITouch *touch = [[event allTouches] anyObject];
        NSLog(@"touches cancelled=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

        [mkMapView touchesCancelled:touches withEvent:event];

    };
    mgr.touchesEndedCallback = ^(NSSet * touches, UIEvent * event) {
        UITouch *touch = [[event allTouches] anyObject];
        NSLog(@"touches ended=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

        [mkMapView touchesEnded:touches withEvent:event];

    };
    mgr.touchesMovedCallback = ^(NSSet * touches, UIEvent * event) {
        UITouch *touch = [[event allTouches] anyObject];
        NSLog(@"touches moved=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);

        [mkMapView touchesMoved:touches withEvent:event];

    };

    [self addGestureRecognizer:mgr];

    return self;
}


@end

I implemented my own gesture recognizer to intercept these events, and I am trying to go to intercepted events through mkMapView. I see log messages, however it does not miss events, since I can scroll / pan mkmapview.

+4
2

, , . , . , , Otherviews, .

0

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    mkMapView = [[MKMapView alloc] initWithFrame:frame];
    [self addSubview:mkMapView];

    UIView * otherView = [[UIView alloc] initWithFrame:frame];
    otherView.backgroundColor = [UIColor clearColor];
    [self addSubview:otherView];

    return self;
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    return mkMapView;
}
0

Source: https://habr.com/ru/post/1528162/


All Articles