MKAnnotationView图像切换的过渡问题

问题描述 投票:0回答:1

我对于MKAnnotationView有2个图像状态,分别是selecteddeselected。问题在于这两个状态之间的转换很差。网上没有太多关于此的内容,通常我在过渡方面遇到麻烦。

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()
        }
  }
}
swift mapkit mkannotation mkannotationview
1个回答
0
投票

我更新了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

© www.soinside.com 2019 - 2024. All rights reserved.