0

Делаю кастомный коллаут для MapBox. Делал по официальному туториалу https://www.mapbox.com/ios-sdk/examples/custom-callout/. Есть два метода:

func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedView: UIView, animated: Bool)

и

func dismissCallout(animated: Bool)

При нажатии на точку на карте должен выполнятся только первый метод, а выполняется почему то сразу оба, т.е. коллаут не успевает появится как тут же пропадает.

Может кто сталкивался, как это решается?

Файл CustomCalloutView.swift:

import Foundation
import Mapbox

class CustomCalloutView: UIView, MGLCalloutView {

var representedObject: MGLAnnotation
var leftAccessoryView = UIView()/* unused */
var rightAccessoryView = UIView()/* unused */
var delegate: MGLCalloutViewDelegate?

let tipHeight: CGFloat = 10.0
let tipWidth: CGFloat = 20.0

let mainBody: UIButton


required init(representedObject: MGLAnnotation) {
    self.representedObject = representedObject
    self.mainBody = UIButton(type: .system)

    super.init(frame: CGRect.zero)

    backgroundColor = UIColor.clear

    mainBody.backgroundColor = backgroundColorForCallout()
    mainBody.tintColor = UIColor.green
    mainBody.contentEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)
    mainBody.layer.cornerRadius = 4.0

    addSubview(mainBody)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedView: UIView, animated: Bool) {
    if !representedObject.responds(to: #selector(getter: UIPreviewActionItem.title)) {
        return
    }

    view.addSubview(self)

    // Prepare title label
    mainBody.setTitle(representedObject.title!, for: .normal)
    mainBody.sizeToFit()

    if isCalloutTappable() {
        // Handle taps and eventually try to send them to the delegate (usually the map view)
        mainBody.addTarget(self, action: #selector(CustomCalloutView.calloutTapped), for: .touchUpInside)
    } else {
        // Disable tapping and highlighting
        mainBody.isUserInteractionEnabled = false
    }

    // Prepare our frame, adding extra space at the bottom for the tip
    let frameWidth = mainBody.bounds.size.width
    let frameHeight = mainBody.bounds.size.height + tipHeight
    let frameOriginX = rect.origin.x + (rect.size.width/2.0) - (frameWidth/2.0)
    let frameOriginY = rect.origin.y - frameHeight
    frame = CGRect(x:frameOriginX, y:frameOriginY, width:frameWidth, height:frameHeight)

    if animated {
        alpha = 0

        UIView.animate(withDuration: 0.2) { [weak self] in
            self?.alpha = 1
        }
    }
}

func dismissCallout(animated: Bool) {
    if (superview != nil) {
        if animated {
            UIView.animate(withDuration: 0.2, animations: { [weak self] in
                self?.alpha = 0
                }, completion: { [weak self] _ in
                    self?.removeFromSuperview()
            })
        } else {
            removeFromSuperview()
        }
    }
}

func isCalloutTappable() -> Bool {
    if let delegate = delegate {
        if delegate.responds(to: #selector(MGLCalloutViewDelegate.calloutViewShouldHighlight)) {
            return delegate.calloutViewShouldHighlight!(self)
        }
    }
    return false
}

func calloutTapped() {
    if isCalloutTappable() && delegate!.responds(to: #selector(MGLCalloutViewDelegate.calloutViewTapped)) {
        delegate!.calloutViewTapped!(self)
    }
}

// MARK: - Custom view styling

func backgroundColorForCallout() -> UIColor {
    return UIColor.darkGray
}

override func draw(_ rect: CGRect) {
    // Draw the pointed tip at the bottom
    let fillColor = backgroundColorForCallout()

    let tipLeft = rect.origin.x + (rect.size.width / 2.0) - (tipWidth / 2.0)
    let tipBottom = CGPoint(x:rect.origin.x + (rect.size.width / 2.0), y:rect.origin.y + rect.size.height)
    let heightWithoutTip = rect.size.height - tipHeight

    let currentContext = UIGraphicsGetCurrentContext()!

    let tipPath = CGMutablePath()
    tipPath.move(to: CGPoint(x: tipLeft, y: heightWithoutTip))
    tipPath.addLine(to: tipBottom)
    tipPath.addLine(to: CGPoint(x:tipLeft+tipWidth, y: heightWithoutTip))
    tipPath.closeSubpath()

    fillColor.setFill()
    currentContext.addPath(tipPath)
    currentContext.fillPath()
}

}

Вот так указываю во ViewController'е что нужно отображать кастомные коллауты:

func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> UIView? {
    return CustomCalloutView(representedObject: annotation)
}
3
  • код покажите .. 10 дек 2016 в 22:12
  • @MaxMikheyenko ответил апдейтом
    – Razorik
    10 дек 2016 в 23:01
  • у вас когда приложение на брейкпоинте останавливается посмотрите backtrace откуда вызов идет 13 дек 2016 в 13:10

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.