【问题标题】:MKAnnotationView image toggling's transition issueMKAnnotationView 图像切换的转换问题
【发布时间】:2019-11-21 22:20:40
【问题描述】:

MKAnnotationView 有 2 个图像状态,它们是 selecteddeselected。问题是这两种状态之间的过渡很差。网上没有太多关于此的内容,而且我通常在转换时遇到问题。

这是我正在使用的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】:

    我将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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多