CallOutAccessory 按钮图像状态

CallOutAccessory button image state

我在标注配件上有一个最喜欢的按钮,通过子类化 UIButton(这处理图像切换)视觉上可以切换。当我四处移动地图时,我假设 视图被重新绘制,切换将 return (通常)到它的原始未切换状态。 有没有人有这种发展的经验?提前致谢。

CallOutAccessory with fave button

您可以在地图上的 viewForAnnotation 方法中处理此问题。假设您已经 subclassed MKAnnotation 并且正在使用可重复使用的 MKAnnotationViews。在您的注释中 class 添加一个标志以确定注释是否被选中并保持更新。然后,在您的 viewForAnnotation 方法中...

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = yourMap.dequeueReusableAnnotationView(withIdentifier: "someIdentifier") as! YourCustomAnnotationClass
    if annotation.isSelected {
        // Setup your button here
    }
    return annotationView
}

基本上,如果您使用可重复使用的注释视图,则必须像集合视图的工作方式一样设置视图。每次调用 viewFor annotation 时,此代码都会 运行 因此,如果您每次都没有手动正确设置它,或者不注意如何重用单元格,就会发生一些奇怪的事情。