gpt4 book ai didi

swift - MKAnnotationView图像切换的过渡问题

转载 作者:行者123 更新时间:2023-11-30 10:31:56 25 4
gpt4 key购买 nike

我的 MKAnnotationView 有 2 个图像状态,分别是选中取消选中。问题是这两种状态之间的过渡很差。网上对此的了解不多,而且我通常在转换方面遇到困难。

enter image description here

这是我正在使用的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()
}

这是从这里的答案中提取的:https://stackoverflow.com/a/54431257/4114335

关于swift - MKAnnotationView图像切换的过渡问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58984618/

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