gpt4 book ai didi

ios - 将事件传递到另一个 View

转载 作者:可可西里 更新时间:2023-11-01 06:15:18 28 4
gpt4 key购买 nike

我有一种情况,我想将一个 UIView(不是我的代码)放在另一个 UIView 的顶部,并将事件传播到最底部的 View 。更具体地说,我想在 MKMapView 之上放置一个 UIView。

我有一个包含两个 View (作为 subview )的 UIView。我希望我的 uiview 将事件从它的 View 转发到它的两个 subview 。

.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

我实现了自己的手势识别器来拦截这些事件,并且我试图将拦截的事件传递给 mkMapView。我看到了日志消息,但是它似乎没有传递事件,因为我可以使 mkmapview 滚动/平移。

最佳答案

如果另一个 View 位于带有附加手势识别器的 View 的顶部,则手势识别器与最顶层 View 一起工作,识别器不会触发。一种快速而肮脏的方法是将同一组识别器分配给 otherView 并在不需要时适本地删除它们。该操作不需要在接收手势的同一 View 上执行,因此在其他 View 中接收平移手势然后在后台平移 map 应该可以工作。

关于ios - 将事件传递到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21941411/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com