点击时如何给出MGLAnnotation反馈?使用Mapbox

时间:2019-07-28 21:50:32

标签: ios swift annotations mapbox mglmapview

点击注释后,我希望它有某种反馈。 (例如抑郁症(如手套上的宽度和高度收缩))。

我尝试通过手势识别器执行此操作,但是我发现它们似乎没有按预期工作,也不允许didselect运行。因此,现在我正在尝试使用this example on mapboxes website使其可以在其中拖动注释的方式工作。

我只有2个问题。 1:动画似乎延迟了,并且2如何防止注释移动。

我一直在使用的代码:

此代码位于内部:class CustomAnnotationView: MGLAnnotationView

    // Custom handler for changes in the annotation’s drag state.
override func setDragState(_ dragState: MGLAnnotationViewDragState, animated: Bool) {
    super.setDragState(dragState, animated: animated)

    switch dragState {
    case .starting:
        print("Starting", terminator: "")
        startDragging()
    case .dragging:
        print(".", terminator: "")
    case .ending, .canceling:
        print("Ending")
        endDragging()
    case .none:
        break
    @unknown default:
        fatalError("Unknown drag state")
    }
}

// When the user interacts with an annotation, animate opacity and scale changes.
func startDragging() {
    UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
        self.layer.opacity = 0.8
        self.transform = CGAffineTransform.identity.scaledBy(x: 1.5, y: 1.5)
    }, completion: nil)

    // Initialize haptic feedback generator and give the user a light thud.
    if #available(iOS 10.0, *) {
        let hapticFeedback = UIImpactFeedbackGenerator(style: .light)
        hapticFeedback.impactOccurred()
    }
}

func endDragging() {
    transform = CGAffineTransform.identity.scaledBy(x: 1.5, y: 1.5)
    UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
        self.layer.opacity = 1
        self.transform = CGAffineTransform.identity.scaledBy(x: 1, y: 1)
    }, completion: nil)

    // Give the user more haptic feedback when they drop the annotation.
    if #available(iOS 10.0, *) {
        let hapticFeedback = UIImpactFeedbackGenerator(style: .light)
        hapticFeedback.impactOccurred()
    }
}

0 个答案:

没有答案