如何在地图视图上添加透明蒙版 [英] How to add a transparent mask on map View
问题描述
我想在我的MapView屏幕上添加一个颜色蒙版(没有坐标,我想在我的所有mapView上显示它),并将控件保留在此mapView上.
我听说过MKOverlay,但是我不知道如何在所有地图上使用它,并且不使用坐标,因为我希望在所有地图屏幕上使用它.
I want to add a color mask on my MapView screen (without coordinate, I want to display it on all my mapView) and keep the control on this mapView.
I've heard about MKOverlay but I dont know how to use it for all the map and without using coordinate cause I want it on all the map screen.
有人有这个主意吗?
推荐答案
您可以通过实现-hitTest:withEvent:
方法来创建过滤器视图.触摸过滤器视图时,它将在返回的视图上起作用:
You can create a filter view with implementing the -hitTest:withEvent:
method. When you touch the filter view, it'll works on the view it returns:
返回包含指定点的视图层次结构中接收者的最远后裔(包括自身).
Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.
假设您有一个名为mapView
的MKMapView和一个名为mapFilterView
的MapFilterView(从UIView继承),它们都是mainView
的子视图,但图层(mapFilterView
在mapView
上)除外.这是一个片段代码,将清楚地描述它:
Suppose you've a MKMapView called mapView
, and a MapFilterView (subclassed from UIView) called mapFilterView
, both of them are subviews of mainView
, except the layer (mapFilterView
is on mapView
). Here's a snippet code that'll describe it clear:
MapFilterView.h
MapFilterView.h
...
@interface MapFilterView : UIView {
MKMapView * mapView_;
}
@property (nonatomic, retain) MKMapView * mapView;
@end
MapFilterView.m
MapFilterView.m
#import "MapFilterView.h"
@implementation MapFilterView
@synthesize mapView = mapView_;
- (void)dealloc {
self.mapView = nil;
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView * child = [super hitTest:point withEvent:event];
if (child == self)
return mapView_;
return child;
}
在主视图控制器中(假设在-viewDidLoad:
中):
And in your main view controller (suppose in -viewDidLoad:
):
// Create the map view
...
[self.view addSubview:self.mapView];
// Create the map filter view
mapFilterView_ = [[MapFilterView alloc] initWithFrame:mapFilterViewFrame];
mapFilterView_.mapView = self.mapView;
[self.view addSubview:mapFilterView_];
此代码仅是示例,您最好自己进行测试.希望这会有所帮助! :)
This code is just a sample, you'd better test it yourself. Hope this'll helps! :)
这篇关于如何在地图视图上添加透明蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!