Swift MKAnnotationView图像切换';中国的过渡问题
对于MKAnnotationView,我有两种图像状态,分别是Swift MKAnnotationView图像切换';中国的过渡问题,swift,mapkit,mkannotation,mkannotationview,Swift,Mapkit,Mkannotation,Mkannotationview,对于MKAnnotationView,我有两种图像状态,分别是已选择和已取消选择。问题是这两个国家之间的过渡很差。网上没有太多关于这方面的信息,我通常在转换方面有困难 以下是我正在使用的MKAnnotationView: class CustomPinView: MKAnnotationView { func updateImage() { guard let mapAnnotation = annotation as? MapAnnotation else {retu
已选择
和已取消选择
。问题是这两个国家之间的过渡很差。网上没有太多关于这方面的信息,我通常在转换方面有困难
以下是我正在使用的MKAnnotationView
:
class CustomPinView: MKAnnotationView {
func updateImage() {
guard let mapAnnotation = annotation as? MapAnnotation else {return}
if let selectedImageName = mapAnnotation.selectedImageName, isSelected {
image = UIImage(inCurrentBundleWithName: selectedImageName)
} else if let imageName = mapAnnotation.imageName {
image = UIImage(inCurrentBundleWithName: imageName)
} else {
image = nil
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
updateImage()
}
override var annotation: MKAnnotation? {
didSet {
updateImage()
}
}
}
我更新了
updateImage
函数,如下所示:
private func updateImage() {
CATransaction.begin()
CATransaction.setAnimationDuration(0.2)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .easeOut))
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseIn, animations: {
guard let mapAnnotation = self.annotation as? MapAnnotation else {return}
if let selectedImageName = mapAnnotation.selectedImageName, self.isSelected {
self.image = UIImage(inCurrentBundleWithName: selectedImageName)
self.layer.anchorPoint = CGPoint(x: 0.5, y: 0.6)
} else if let imageName = mapAnnotation.imageName {
self.image = UIImage(inCurrentBundleWithName: imageName)
self.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
} else {
self.image = nil
}
self.centerOffset = CGPoint(x: 0.5, y: 0.5)
}, completion: nil)
CATransaction.commit()
}
这是从以下答案中提取的: