点击时自定义注释 swift 的边框向外(不是向内)
Border out (not IN) of custom annotation swift when tapped
点击 MapView 中的自定义我的注释视图(Pin)时,我需要在我的注释外加白色边框。
我还需要在阴影中,点击时。但是我怎么能做到呢? (当我取消选择注释时必须保持正确的样式)。如果我用:
var newFrame: CGRect = view.frame; newFrame = newFrame.insetBy(dx: -borderWidth, dy: -borderWidth); view.frame = 新框架;在 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
}
如果您实施以下两种方法来检测注释何时为 selected/deselected,您可以 draw/remove 一个白色圆圈作为注释下方的 UIView
。首先在您的视图控制器中,设置 mapView
的委托。
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
创建一个白色圆圈。 这个白色圆圈视图有一个标签1
,方便需要删除的时候。然后,此视图将作为子视图插入,该子视图将注释的视图作为索引零处的父视图,以使其显示在注释后面。 didDeselect
方法通过引用标记来删除圆。
就个人而言,最好插入一个白色圆圈作为 UIImageView
,而不是从 UIView
制作一个圆圈,因为这样 hack-ish.
演示
点击 MapView 中的自定义我的注释视图(Pin)时,我需要在我的注释外加白色边框。
我还需要在阴影中,点击时。但是我怎么能做到呢? (当我取消选择注释时必须保持正确的样式)。如果我用:
var newFrame: CGRect = view.frame; newFrame = newFrame.insetBy(dx: -borderWidth, dy: -borderWidth); view.frame = 新框架;在 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
}
如果您实施以下两种方法来检测注释何时为 selected/deselected,您可以 draw/remove 一个白色圆圈作为注释下方的 UIView
。首先在您的视图控制器中,设置 mapView
的委托。
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
创建一个白色圆圈。 这个白色圆圈视图有一个标签1
,方便需要删除的时候。然后,此视图将作为子视图插入,该子视图将注释的视图作为索引零处的父视图,以使其显示在注释后面。 didDeselect
方法通过引用标记来删除圆。
就个人而言,最好插入一个白色圆圈作为 UIImageView
,而不是从 UIView
制作一个圆圈,因为这样 hack-ish.
演示