gpt4 book ai didi

ios - 点击时自定义注释的边框会 swift 超出(而不是进入)

转载 作者:行者123 更新时间:2023-11-30 11:52:46 29 4
gpt4 key购买 nike

当点击 MapView 中的自定义注释 View (Pin)时,我需要在注释中添加白色边框。

(left - tapped, right - not tapped

此外,点击时我需要处于阴影中。但我怎样才能做到呢? (当我取消选择注释时必须保持正确的样式)。如果我做到了:

var newFrame: CGRect = view.frame; newFrame = newFrame.insetBy(dx: -borderWidth, dy: -borderWidth); view.frame = newFrame;在 mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 方法中并设置边框 - 我的图像也会放大,但这是不对的。

// My custom annotation
class CustomPointAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var restoInfo: Restaurant?

init(resto: Restaurant) {
self.coordinate = resto.coordinate!
self.restoInfo = resto
}
}

// And func adding custom annotation on MapView
var restorationPin: CustomPointAnnotation!
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
let cpa = annotation as? CustomPointAnnotation
if cpa?.restoInfo == nil {
return nil
}

let annotationView = MKAnnotationView(annotation: restorationPin, reuseIdentifier: "id")

let icon = cpa?.restoInfo?.encodeBase64toImage(base64: (cpa?.restoInfo?.logoMap)!)

annotationView.image = icon
annotationView.layer.masksToBounds = true
annotationView.contentMode = .scaleAspectFill
annotationView.frame.size = CGSize(width: 45, height: 45)
annotationView.layer.cornerRadius = 22.5
return annotationView
}

最佳答案

如果您实现以下两种方法来检测何时选择/取消选择注释,则可以在注释下方绘制/删除一个白色圆圈作为UIView。首先在 View Controller 中,设置 mapView 的委托(delegate)。

self.mapView.delegate = self

下面是检测何时单击注释的两种方法。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
/*Make a white circle*/
let whiteBackground = UIView(frame: CGRect(x: -27, y: -55, width: 80, height: 80))
whiteBackground.backgroundColor = UIColor.white
whiteBackground.layer.cornerRadius = 40

/*Set circle's tag to 1*/
whiteBackground.tag = 1
/*Add the circle beneath the annotation*/
view.insertSubview(whiteBackground, at: 0)
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
/*Removing the white circle*/
view.viewWithTag(1)?.removeFromSuperview()
}

解释。单击注释时,将调用 didSelect 方法,并从 UIView 创建一个白色圆圈。 这个白色圆圈 View 有一个 1 标签,以便在需要删除时方便。然后,该 View 将作为 subview 插入,其中注释的 View 作为索引零处的 super View ,以使其显示在注释后面。 didDeselect 方法通过引用标记来删除圆圈。

就个人而言,您最好只插入一个白色圆圈作为 UIImageView 而不是从 UIView 制作一个圆圈,因为它不太像 hack-ish 。

演示

Map Annotation with white circle

关于ios - 点击时自定义注释的边框会 swift 超出(而不是进入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160479/

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