我有一个带有图钉位置的地图视图(坐标取自CoreData),我需要在选择图钉时显示照片(使用其URL和AFNetworking)。
选择图钉时,我需要保存两个图片图像和照片图片。
如果选择了下一个引脚,则取消选择前一个引脚,以便从屏幕上删除照片并添加相应引脚的另一张照片。
这是一张显示我特别需要的图片:
因此,针脚在屏幕上,照片在屏幕上。
问题:
解决第一个和第二个问题:
我已经制作了一个自定义类的MKPinAnnotationView,它负责将imageView添加到MKPinAnnotationView,以便在屏幕上保留图片和照片。
#import "PSMKPinAnnotationView.h"
//my custom annotations, I keep URLs and coordinates of photos there.
#import "PSMapAnnonation.h"
#import "UIImageView+AFNetworking.h" //to display images by URL
@interface PSMKPinAnnotationView ()
@property (nonatomic, strong) UIImageView *imageViewForAnnotaion;
//property in header is:
//@property (nonatomic, getter = isDetailViewHidden) BOOL detailViewHidden;
@end
@implementation PSMKPinAnnotationView
- (BOOL)validAnnotation
{
return [self.annotation isKindOfClass:[PSMapAnnonation class]];
}
- (void)setDetailViewHidden:(BOOL)detailViewHidden
{
//draw image in custom view
if (detailViewHidden==NO)
{
if ([self validAnnotation])
{
self.annotation=(PSMapAnnonation*)self.annotation;
CGRect rect=CGRectMake(0, -35, 30, 30); // y=-35 to make correct offset
self.imageViewForAnnotaion = [[UIImageView alloc] initWithFrame:rect];
[self.imageViewForAnnotaion setImageWithURL:
[(PSMapAnnonation*)self.annotation imageURL]];
[self setClipsToBounds:NO];
[self addSubview:self.imageViewForAnnotaion];
}
}
else if (detailViewHidden==YES)
{
if ([self validAnnotation])
{
[self.imageViewForAnnotaion removeFromSuperview]];
}
}
}
在ViewController中,我的mapview是mapView的方法,我正在这样做:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(PSMKPinAnnotationView
*)view
{
if ([view.annotation isKindOfClass:[MKUserLocation class ]]) return;
view.detailViewHidden=NO;
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
if (![view isKindOfClass:[PSMKPinAnnotationView class]]) {
return; //blue point which describes currecnt user position is a view pin too
//and in case of being selected without this check app will crash.
}
PSMKPinAnnotationView *customAnnotationView=(PSMKPinAnnotationView*)view;
[customAnnotationView setDetailViewHidden:YES];
}
所以,我已经解决了前两个问题:
结果如下:
但是在搜索MKMapView
的触摸视图时检测到了第三个问题#pragma mark - Touches
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch");
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.mapView];
for (UIView *view in self.mapView.subviews)
{
if ([view isKindOfClass:[PSMKPinAnnotationView class]] &&
CGRectContainsPoint(view.frame, touchLocation))
{
NSLog(@"%@",[view class]);
}
NSLog(@"%@",[view class]);
}
}
日志向我展示了一些Apple的私人课程:
- 2014-06-19 11:00:45.428 PhotoShare[881:60b] touch
- 2014-06-19 11:00:45.428 PhotoShare[881:60b] UIView
- 2014-06-19 11:00:45.428 PhotoShare[881:60b] MKAttributionLabel
- 2014-06-19 11:01:47.574 PhotoShare[881:60b] touch
- 2014-06-19 11:01:47.575 PhotoShare[881:60b] UIView
- 2014-06-19 11:01:47.575 PhotoShare[881:60b] MKAttributionLabel
如果触摸我的照片(其图片视图),我的自定义类视图未被检测到,并且显示了一些MKAttributionLabel和UIView
有没有简单的解决方案来做这些事情?
如何将触摸连接到添加的imageView并在视图中找到它们? 日志向我展示了MapKit添加了一些自己的视图 MKAttributionLabel(Apple的私人课程)?