草庐IT

objective-c - MKAnnotationView 帧大小在缩放之前不会更新 MapView

coder 2024-01-11 原文

我正在尝试实现一个带有自定义按钮的自定义 MKAnnotationView。我不想使用 MKAnnotation 的标注属性,因为无法访问该 View 的框架。相反,我使用 MKAnnotationView View 来添加我的 UIView,其中包含我的自定义标注气泡。一切正常,即使触摸被按钮捕获。唯一的问题是调整 MKAnnotationView 框架的大小。我能够在我的自定义 MKAnnotationView 中的 (void)setSelected:(BOOL)selected animated:(BOOL)animated 方法期间执行此操作,该方法是 MKAnnotationView 的子类,但是当调整大小的 View 应显示在 MapView 时,这只能通过放大或缩小 map 来实现。我试过传递诸如 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);
}

}

最佳答案

我已经设法让一切正常运转。我很确定我还没有找到完美的方法来做到这一点,但它符合我的需求并且最终没有影响用户体验。

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

在注意到我注意到我能够将 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,该标注气泡内部有一个按钮,但我无法在整个互联网上找到适合我需要的任何东西。该解决方案有效。这不是完美的实现,但可以完成工作。我希望能帮助别人。

关于objective-c - MKAnnotationView 帧大小在缩放之前不会更新 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770528/

有关objective-c - MKAnnotationView 帧大小在缩放之前不会更新 MapView的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  3. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  4. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  9. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  10. HBase Region 简介和建议数量&大小 - 2

    Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile

随机推荐