gpt4 book ai didi

ios - Mapbox Callout View 在顶部的正确定位

转载 作者:行者123 更新时间:2023-11-29 11:38:53 24 4
gpt4 key购买 nike

我想让标注框位于位置标记的顶部,位置标记是框左上角的黑色标记。

这是我目前的代码:

import Mapbox
import UIKit

class CustomCalloutView: UIView, MGLCalloutView {
var representedObject: MGLAnnotation

// Allow the callout to remain open during panning.
let dismissesAutomatically: Bool = false
let isAnchoredToAnnotation: Bool = true

// https://github.com/mapbox/mapbox-gl-native/issues/9228
override var center: CGPoint {
set {
var newCenter = newValue
newCenter.y = newCenter.y - bounds.midY
super.center = newCenter
}
get {
return super.center
}
}

lazy var leftAccessoryView = UIView() /* unused */
lazy var rightAccessoryView = UIView() /* unused */

weak var delegate: MGLCalloutViewDelegate?

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

let mainBody: UIView

required init(representedObject: MGLAnnotation) {
self.representedObject = representedObject
self.mainBody = CustomDetailsView().loadNib()

super.init(frame: .zero)

backgroundColor = .clear
autoresizesSubviews = true
mainBody.backgroundColor = .white
mainBody.layer.cornerRadius = 4.0

addSubview(mainBody)
}

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


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

view.addSubview(self)
mainBody.sizeToFit()

// Prepare our frame, adding extra space at the bottom for the tip
let button = UIButton(type: .system)
let frameWidth = button.bounds.size.width
let frameHeight = button.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()
}
}
}

// MARK: - Callout interaction handlers

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

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

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 - 1

let currentContext = UIGraphicsGetCurrentContext()!

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

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

enter image description here

我似乎无法找到将标注定位在图钉顶部的方法。有没有办法用当前的代码设置实现相同的结果?

最佳答案

我最终不得不使用以下代码片段对 mainBody.frame 的定位方式做一些更改:

// 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
self.frame = CGRect(x: frameOriginX, y: frameOriginY, width: frameWidth, height: frameHeight)
mainBody.frame.size.width = frameWidth
mainBody.frame.size.height = frameHeight - tipHeight

关于ios - Mapbox Callout View 在顶部的正确定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47674994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com