基于 Swift 多地圖源業(yè)務(wù)向地圖控件實現(xiàn)(三):交互事件處理

有的時候地圖控件上還會有交互需求。比如在我的業(yè)務(wù)場景里,需要編輯無人機巡航的區(qū)域。要編輯區(qū)域,區(qū)域的多邊形頂點就需要可以拖動。

image

簡化一下需求,我們現(xiàn)在來實現(xiàn)一下點拖動編輯的功能。

首先我們定義一下可以移動的頂點 View:

class MapMoveableAnnotation: UIImageView {
    
    init(type: MoveableAnnotationType) {
        self.type = type
        super.init(frame: CGRect.zero)
        isUserInteractionEnabled = true
        setupUI()
    }
    
    private func setupUI() {
        bounds = CGRect(x: 0, y: 0, width: 44, height: 44)
        contentMode = .center
        image = Asset.Map.iconAreaEditPoint.image
        highlightedImage = Asset.Map.iconAreaEditPointSelected.image
    }
}

上面要注意的細節(jié)是需要設(shè)置 isUserInteractionEnabled 為 true。

接著我們定義一個 view 專門響應用戶的交互事件,因為要首先響應交互,這個 view 要在地圖控件的最頂層。

class MapInteractionView: UIView {
    
     override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        for subview in subviews {
            let subPoint = subview.convert(point, from: self)
            if let result = subview.hitTest(subPoint, with: event) {
                return result
            }
        }
        return nil
    }
}

因為 MapInteractionView 里所有元素都會響應交互事件,因此直接重新實現(xiàn)了 hitTest 方法。如果交互的點在內(nèi)部元素上響應事件,如果不在的話不響應,讓交互事件可以傳到下層的地圖源響應。

接下來我們在 MapInteractionView 中定義添加可拖動控制點的方法:

class MapInteractionView: UIView {
    
    private var areaControlAnnotations: [MapMoveableAnnotation] = []
    
    func renderAreaEdit(vertexs: [CGPoint]) {
        if areaControlAnnotations.count != vertexs.count {
            areaControlAnnotations.forEach { $0.removeFromSuperview() }
            areaControlAnnotations.removeAll()
            areaControlAnnotations = vertexs.map { _ in MapMoveableAnnotation() }
            areaControlAnnotations.forEach {
              addSubview($0)
            }
        }

        for (index, point) in vertexs.enumerated() {
            areaControlAnnotations[index].center = point
            areaControlAnnotations[index].tag = index
        }
    }
}

接著我們給 MapInteractionView 添加手勢響應:

protocol MapInteractionDelegate: class {
    func areaVertexMoved(index: Int, point: CGPoint, isFinish: Bool)
    func areaFinishChanging()
}

class MapInteractionView: UIView {

    weak var delegate: MapInteractionDelegate?

    private var panGesture: UIPanGestureRecognizer?

    override init(frame: CGRect) {
       super.init(frame: frame)
       panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
            addGestureRecognizer(panGesture!)
    }

    private var panSelectedView: UIView?

    @objc private func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
        switch gesture.state {
        case .began:
            guard let selectedView = hitTest(gesture.location(in: self), with: nil) else { return }
            panSelectedView = selectedView
            if let movableAnnotation = selectedView as? MapMoveableAnnotation {
                movableAnnotation.isHighlighted = true
            }
        case .changed:
            guard let panSelectedView = panSelectedView else { return }
            let touchPoint = gesture.location(in: self)
            panSelectedView.center = touchPoint
            delegate?.areaVertexMoved(index: panSelectedView.tag, point: touchPoint, isFinish: false)
        case .ended:
            if let movableAnnotation = panSelectedView as? MapMoveableAnnotation {
                movableAnnotation.isHighlighted = false
                if movableAnnotation.type == .areaControlPoint {
                    delegate?.areaVertexMoved(index: movableAnnotation.tag, point: movableAnnotation.center, isFinish: true)
                }
                delegate?.areaFinishChanging()
            }
            panSelectedView = nil
        default:
            break
        }
    }
}

這里要強調(diào)的還是每個類職責的劃分。在架構(gòu)中我們常說單一職責,單一職責對于維護性的影響至關(guān)重要。我們對 MapInteractionView 的定義就是接受用戶的交互事件。因此它在上面的場景中,處理移動手勢,通知外界哪個頂點開始移動,移動結(jié)束。

如何檢驗單一職責執(zhí)行的好不好呢?我的一個建議是思考這個模塊是否可以抽離出來獨立驗證,也就是可測試性。以 MapInteractionView 為例,現(xiàn)在這樣設(shè)計如果想要單獨測試這個類,可以做到嗎?相對而言是比較容易的,可以在單元測試項目中初始化添加到 VC 中,輸入特定的坐標點,接著在模擬控制坐標點位置模擬拖動手勢。在 MapInteractionDelegate 中斷言返回的結(jié)果和模擬的手勢值是否一致。

最后一環(huán)就是把 MapInteractionView 添加到地圖控件的最頂層,這塊代碼比較簡單代碼我就不列舉了。

總結(jié)

復雜的業(yè)務(wù)必然會有“復雜”的實現(xiàn)。架構(gòu)設(shè)計要解決的是:面對復雜的需求,復雜的實現(xiàn)應該讓開發(fā)者容易理解,容易維護。怎樣做到呢?我們在規(guī)劃時就劃分好各個模塊,讓各個模塊通過合理的組合實現(xiàn)復雜的功能。這樣開發(fā)者在維護的時候只需要關(guān)注兩件事:1.模塊是如何劃分的 2.我要維護的部分在模塊中是如何實現(xiàn)的。每個模塊因為職責清晰,所以即便不清楚全局,也可以維護這個模塊的實現(xiàn)細節(jié)。這樣的設(shè)計就達到了易理解、已維護的目標。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容