这是我之前的线程,我将图像设置为注释视图引脚
Swift - 设置从数组到注释引脚的不同图像 https://stackoverflow.com/questions/28033159/swift-setting-different-images-from-array-to-annotation-pins/28033880#28033880
现在我遇到了在注释视图图像上设置圆角的问题。
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
}
预先感谢您的任何帮助!
我找到了解决方案,我认为它非常优雅。
不要更改注释的图层,而是创建一个 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)