【问题标题】:Swift - setting rounded corners to annotation view imageSwift - 为注释视图图像设置圆角
【发布时间】:2015-01-20 12:48:41
【问题描述】:

这是我之前的帖子,我将图像设置为注释视图引脚

Swift - setting different images from array to annotation pins

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

cannot show callout with clipsToBounds enabled swift

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

    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
}

提前感谢您的帮助!

【问题讨论】:

  • 看看这个自定义调用演示 - stackoverflow.com/questions/27519517/…
  • 我想情况有点不同,因为他试图在标注字段内显示图像,我用图像更改了图钉,并且没有圆角一切正常。

标签: ios swift mapkit mkannotation mkannotationview


【解决方案1】:

我找到了解决方案,我认为它非常优雅。

与其更改注释的层,不如创建一个 UIImageView,将图像附加到其上并将其作为视图添加到注释中。

    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

【讨论】:

  • 别针不显示 :(
【解决方案2】:

只是为了增加头晕的好答案:

添加图像子视图效果很好,但无法再单击图钉。 我发现更改图钉的大小以匹配图像可以解决此问题。

anView.frame = imageView.frame

您可能还想移动标注,具体取决于图片的大小。

anView.calloutOffseet = CGPoint(x: 0, y: -10)

【讨论】:

    【解决方案3】:

    另一种方法是添加一个子层而不是像这样的 imageView:

    let imageLayer = CALayer()
    imageLayer.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    imageLayer.contents = image?.cgImage
    imageLayer.cornerRadius = imageLayer.frame.size.width / 2
    imageLayer.masksToBounds = true
    imageLayer.borderWidth = 4.0
    imageLayer.borderColor = UIColor.white.cgColor
    anView.layer.addSublayer(imageLayer)
    anView.frame = imageLayer.frame
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      相关资源
      最近更新 更多