【问题标题】:MKAnnotationView frame size won't update MapView until zoomMKAnnotationView 帧大小在缩放之前不会更新 MapView
【发布时间】:2012-05-26 23:28:18
【问题描述】:

我正在尝试实现一个带有自定义按钮的自定义 MKAnnotationView。我不想使用 MKAnnotation 的标注属性,因为无法访问该视图的框架。相反,我使用 MKAnnotationView 视图来添加我的 UIView,其中包含我的自定义标注气泡。一切正常,即使是触摸也被按钮捕获。唯一的问题是调整 MKAnnotationView 框架的大小。我可以在我的自定义 MKAnnotationView 中的 (void)setSelected:(BOOL)selected animated:(BOOL)animated 方法中做到这一点,该方法是 MKAnnotationView 的子类,但是当调整大小的视图应该显示在 MapView 上时,只能通过放大或缩小地图来实现。我试过传递诸如needsLayoutneedsDisplay 之类的东西,但没有成功。

我做错了什么? MapView在放大缩小时调用的方法是什么,所以我可以调用它来刷新自定义的MKAnnotationView框架?

这是我创建的 CustomMKAnnotationView 类的一些代码:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

[super setSelected:selected animated:animated];

if (selected) { 
     self.frame = CGRectMake(0, 0, 170, 66);
     self.backgroundColor = [UIColor lightTextColor];
     self.centerOffset = CGPointMake(54.5, -33);
     self.image = nil;
     [self createBubble];
     [self animateIn];
     [self addSubview:bubbleView];
}else{
     [bubbleView removeFromSuperview];
     self.frame = CGRectMake(0, 0, 13, 17);
     self.centerOffset = CGPointMake(0, 0);
}

}

【问题讨论】:

    标签: objective-c ios mkannotation mkannotationview


    【解决方案1】:

    我已经设法让事情顺利进行。我很确定我还没有找到完美的方法来做到这一点,但它符合我的需求,最终并没有影响用户体验。

    我发现框架实际上已添加到视图中,但由于我已将 frame origin 设置为 0,0,因此视图已添加到我正在检查的缩放位置之外。

    在注意到我注意到我能够将frame 更改为我想要自定义气泡所在的正确位置之后。但是当我放大或缩小时,frame 重新定位到不同的位置。原来centerOffset 属性正在这样做,所以我必须匹配framecenterOffset 属性以使它们指向屏幕上完全相同的点,结果非常令人满意。用户体验是流畅的。

    这是我创建的MKAnnotationView 子类下的代码:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    
    if (selected) {
    
        //here's the trick: setting the frame to a new position and with different size.
        //as the bubble view is added here and matching that point in view with the
        //centerOffset position. The result looks doesn't affect user experience.
        CGRect frame;
        frame = self.frame;
        frame.size = CGSizeMake(170, 66);
        frame.origin = CGPointMake((self.frame.origin.x)-16.5,(self.frame.origin.y)-49);
        self.frame = frame;
        self.centerOffset = CGPointMake(61, -24.5);
    
        //remove the pin image from the view as the bubble view will be added
        self.image = nil;
    
        [self createBubble];
        [self animateIn];
        [self addSubview:bubbleView];
    
    }else {
    
        [bubbleView removeFromSuperview];
    
        //reset the center offset and the frame for the AnnotationView to reinsert the pin image
        self.centerOffset = CGPointMake(0, 0);
        CGRect frame;
        frame = self.frame;
        //this is the size of my pins image
        frame.size = CGSizeMake(14, 17);
        frame.origin = CGPointMake((self.frame.origin.x)+16.5 ,(self.frame.origin.y)+49);
        self.frame = frame;
    
        //discover whats the point view image (I have different pin colors here)
        UIImage *pointImage = [[UIImage alloc] init];
    
        if ([flagStyle isEqualToString:@"friends"]) {
    
            pointImage = [UIImage imageNamed:@"point_f.png"];
    
        }else if([flagStyle isEqualToString:@"world"]){
    
            pointImage = [UIImage imageNamed:@"point_w.png"];        
    
        }else if([flagStyle isEqualToString:@"my"]){
    
            pointImage = [UIImage imageNamed:@"point_m.png"];        
    
        }
    
    
        self.image = pointImage;
    
    
    }
    
    }
    

    我一直在寻找一种解决方案来创建带有标注气泡的自定义 MKAnnotationView,其中有一个按钮,但我无法在互联网上找到任何适合我需要的东西。此解决方案有效。这不是完美的实现,但可以完成工作。我希望能帮助别人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多