ios - 如何为 MKMapKit View 编写代码当用户单击 pin 时,它会显示另一个容器 View ?

标签 ios objective-c iphone mkmapview

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 7 年前

我想要 ios 应用程序的这种类型的输出。我不知道该怎么做。如果有人知道怎么做,请告诉我。

enter image description here

编辑

我已经根据答案更新了我的代码,但它仍然无法正常工作。我无法理解 UIView 的显示方式及其大小?

#import <UIKit/UIKit.h>
@interface MyView : UIView

@end

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        // initilize all your UIView components
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20,30, 200, 44)];
        label1.text = @"i am label 1";
        [self addSubview:label1]; //add label1 to your custom view

        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20,80, 200, 44)];
        label2.text = @"i am label 2";
        [self addSubview:label2]; //add label2 to your custom view


        }
    return self;
}

=================================================

#import <MapKit/MapKit.h>
#import "MyView.h"

@interface MyCustomView : MKAnnotationView

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event;
- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated;
- (void)setShowCustomCallout:(BOOL)showCustomCallout
;

@property(strong, nonatomic) MyView *calloutView ;

@end

#import "MyCustomView.h"

@implementation MyCustomView

- (void)setShowCustomCallout:(BOOL)showCustomCallout
{
    [self setShowCustomCallout:showCustomCallout animated:NO];
}

- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated
{
    //if (showCustomCallout == showCustomCallout) return;

    showCustomCallout = showCustomCallout;

    void (^animationBlock)(void) = nil;
    void (^completionBlock)(BOOL finished) = nil;

    if (showCustomCallout)
    {

        self.calloutView.alpha = 0.0f;


        animationBlock = ^{
            self.calloutView.alpha = 1.0f;

            [self addSubview:self.calloutView];
        };

    } else {
        animationBlock = ^{ self.calloutView.alpha = 0.0f; };
        completionBlock = ^(BOOL finished) { [self.calloutView removeFromSuperview]; };
    }

    if (animated) {
        [UIView animateWithDuration:0.2f animations:animationBlock completion:completionBlock];

    } else {
        animationBlock();
        completionBlock(YES);
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];

    if ([view isKindOfClass:_calloutView.class]) {
        return nil; // todo: add a new delegate method to the map protocol to handle callout taps
    } else {
        return view;
    }
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    {
        for (UIView *view in self.subviews)
        {
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        }
    }
    return isInside;
}

=================================================
#import "ViewController.h"

@interface ViewController ()
{
    CLLocationManager *locationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    [locationManager requestWhenInUseAuthorization]; // Add This Line
    [locationManager startUpdatingLocation];

    _mapView.showsUserLocation = YES;
    _mapView.delegate = self;

    CLLocationCoordinate2D annotationCoord;

    annotationCoord.latitude = 23.041261;
    annotationCoord.longitude = 72.513892;


    _mapView.region = MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800);

    // MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800)];

    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = @"I am here";
    annotationPoint.subtitle = @"Microsoft's headquarters";


}


- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annView.pinColor = MKPinAnnotationColorGreen;
    return annView;

}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
        [((MyCustomView *)view) setShowCustomCallout:NO animated:YES];
}


-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MyCustomView *annotationView = [[MyCustomView alloc]init];

            [annotationView setShowCustomCallout:YES animated:YES];
}

最佳答案

您必须制作一个自定义 View 才能在 mapview 上制作自定义标注。您必须制作一个包含自定义 View 的类,该类将在 pin tapped 上。这是github上的示例代码。

https://gist.github.com/ShadoFlameX/7495098

关于ios - 如何为 MKMapKit View 编写代码当用户单击 pin 时,它会显示另一个容器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32756922/

上一篇:ios - 是否可以将同一个 ViewController.view 作为单例添加到不同的 View 中作为 subview ?

下一篇:ios - 如何在 iOS 9 中从 Live Photo 获取单张图像

相关文章:

ios方法参数分配全局变量

iphone - 在 UIWebview 中重新加载 gif 图像

ios - Swift:其 View 不在窗口层次结构中

ios - 实现 tapjoy 分析代码后可达性代码中的应用程序崩溃

每个设计的 iPhone Objective-C

iphone - 这是什么意思?初始化丢弃来自指针目标类型的限定符

iphone - 在 App Purchase 沙箱问题

iphone - 我可以使用 Google Analytics 来整理我的 iOS 应用程序的统计信息吗?

iphone - 增强现实与 iPhone 3GS 和 UIView

ios - 应用因 Facebook SDK 和 Flurry SDK 中的 advertisingIdentifier 而被拒绝