Swift – 将圆angular设置为注释视图图像
这是我以前的线程,我把图像设置为注释视图引脚
Swift – 将arrays中的不同图像设置为注释引脚
现在我遇到了在注解视图图像上设置圆angular的问题。
cannot show callout with clipsToBounds enabled swift
似乎我必须select圆angular或显示标注,我真的不明白为什么这两个不能来。 有没有办法做到这一点? 这里是我的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 }
提前感谢任何帮助!
我find了这个解决scheme,我认为是相当优雅。
而不是改变注解层,创build一个UIImageView,附加一个图像,并添加它作为视图的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
只要添加到dizzie的伟大的答案:
添加图像子视图效果很好,但不能再点击图钉。 我发现改变引脚的大小来匹配图像可以解决这个问题。
anView.frame = imageView.frame
您可能还想要移动标注,具体取决于图像的大小。
anView.calloutOffseet = CGPoint(x: 0, y: -10)