gpt4 book ai didi

ios - Swift - 将圆角设置为注释 View 图像

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:32 26 4
gpt4 key购买 nike

这是我之前的帖子,我在其中将图像设置为注释 View 图钉

Swift - setting different images from array to annotation pins

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

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,将图像附加到它,并将其作为 View 添加到注释中。

    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)

如果对您有用,请告诉我。

ZZ

关于ios - Swift - 将圆角设置为注释 View 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com