Swift - 将圆角设置为注释视图图像 [英] Swift - setting rounded corners to annotation view image

查看:113
本文介绍了Swift - 将圆角设置为注释视图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前的帖子,我将图片设置为注释视图图钉

Here is my previous thread, where I set image to annotation view pins

Swift - 将不同的图像从数组设置到注释引脚

现在我在注释视图图像上设置圆角时遇到了问题。

Right now I encountered a problem with setting rounded corners on annotation view image.

cannot show callout with clipsToBounds enabled swift

似乎我必须在圆角或显示标注之间做出选择,我真的不明白为什么这两个不能出现。有没有办法做到这一点?
这是我的viewForAnnotation函数:

appears that I have to choose between rounded corners or showing callout, I don't really get why these two can't come along. Is there any way to do that? Here is my viewForAnnotation function:

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is RestaurantAnnotation) {
        return nil
    }

    let reuseId = "restaurant"
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = false
    }
    else {
        anView.annotation = annotation

    }

    let restaurantAnnotation = annotation as RestaurantAnnotation

    if (restaurantAnnotation.image != nil) {

                        anView.image = restaurantAnnotation.image!

                        anView.layer.masksToBounds = true

                        anView.layer.cornerRadius = (anView.frame.size.height)/10.0

    }
    else {
        // Perhaps set some default image
    }

    return anView
}

提前感谢您的帮助!

推荐答案

我找到了解决方案,我觉得它非常优雅。

I found the solution for this and is it quite elegant I think.

不是更改注释层,而是创建一个UIImageView,将图像附加到它上面并将其添加为视图annoation。

Rather than changing the layer of the annotation, create a UIImageView, attach an image to it and add it as view to the annoation.

    let imageView = UIImageView(frame: CGRectMake(0, 0, 25, 25))
    imageView.image = UIImage(named: cpa.imageName);
    imageView.layer.cornerRadius = imageView.layer.frame.size.width / 2
    imageView.layer.masksToBounds = true
    anView.addSubview(imageView)

请告诉我是否适合您。

DZ

这篇关于Swift - 将圆角设置为注释视图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆