How to add a transparent mask on map View [closed]

2020-07-30 02:54发布

问题:

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 7 years ago.

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.

Does someone have the idea?

回答1:

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.

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

...
@interface MapFilterView : UIView {
  MKMapView * mapView_;
}
@property (nonatomic, retain) MKMapView * mapView;
@end

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;
}

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! :)



回答2:

You can overlay a view on top of the map view. It should not be a subview of the map view, but of its superview - but in front of the map view. If the overlay view has a translucent background color, we can see through it. If the overlay view has user interactions disabled (userInteractionEnabled = NO), touches will fall through to the map view - as if the overlay view weren't there, which is exactly what you seem to want. In other words, it will appear as if the map itself is shaded by your translucent color.

This has nothing to do with map view or mkoverlay. You should fix your tags accordingly. This is simply a question about overlaying any view with a color cast or other visual modification.