- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想自定义单击 MKAnnotationView 时可视化的 iOS8 MapView 标注气泡。默认气泡有点限制(只有标题、副标题和 2 个附件 View ),所以我正在努力寻找替代解决方案。这里有两种可能的方法和我面临的相关问题:
问题 1) 创建自定义标注气泡
挖Apple documentation我发现了这个:
When you use a custom view instead of a standard callout, you need to do extra work to make sure your callout shows and hides appropriately when users interact with it. The steps below outline the process for creating a custom callout that contains a button:
Design an NSView or UIView subclass that represents the custom callout. It’s likely that the subclass needs to implement the drawRect: method to draw your custom content. Create a view controller that initializes the callout view and performs the action related to the button. In the annotation view, implement hitTest: to respond to hits that are outside the annotation view’s bounds but inside the callout view’s bounds, as shown in Listing 6-7. In the annotation view, implement setSelected:animated: to add your callout view as a subview of the annotation view when the user clicks or taps it. If the callout view is already visible when the user selects it, the setSelected: method should remove the callout subview from the annotation view (see Listing 6-8). In the annotation view’s initWithAnnotation: method, set the canShowCallout property to NO to prevent the map from displaying the standard callout when the user selects the annotation. Listing 6-7 shows an example of implementing hitTest: to handle hits in the callout view that might be outside the bounds of the annotation view.
Listing 6-7 Responding to hits within a custom callout
- (NSView *)hitTest:(NSPoint)point
{
NSView *hitView = [super hitTest:point];
if (hitView == nil && self.selected) {
NSPoint pointInAnnotationView = [self.superview convertPoint:point toView:self];
NSView *calloutView = self.calloutViewController.view;
hitView = [calloutView hitTest:pointInAnnotationView];
}
return hitView;
}
Listing 6-8 shows an example of implementing setSelected:animated: to animate the arrival and dismissal of a custom callout view when the user selects the annotation view.
Listing 6-8 Adding and removing a custom callout view
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
// Get the custom callout view.
NSView *calloutView = self.calloutViewController.view;
if (selected) {
NSRect annotationViewBounds = self.bounds;
NSRect calloutViewFrame = calloutView.frame;
// Center the callout view above and to the right of the annotation view.
calloutViewFrame.origin.x = -(NSWidth(calloutViewFrame) - NSWidth(annotationViewBounds)) * 0.5;
calloutViewFrame.origin.y = -NSHeight(calloutViewFrame) + 15.0;
calloutView.frame = calloutViewFrame;
[self addSubview:calloutView];
} else {
[calloutView.animator removeFromSuperview];
}
}
NSView *calloutView = self.calloutViewController.view;
最佳答案
calloutViewController 是处理事件的自定义标注 View 的一部分。您不会在 MapKit 或其他地方找到它。
苹果的说明很好。要创建自己的标注,您应该按照以下步骤操作:
1. Create custom MKAnnotationView or MAPinAnnotationView
2. Override setSelected and hitTest methods in your annotation
3. Create your own callout view
4. Override hitTest and pointInside in you callout view
5. Implement MapView delegate methods didSelectAnnotationView, didDeselectAnnotationView
class MapPin: MKAnnotationView {
class var reuseIdentifier:String {
return "mapPin"
}
private var calloutView:MapPinCallout?
private var hitOutside:Bool = true
var preventDeselection:Bool {
return !hitOutside
}
convenience init(annotation:MKAnnotation!) {
self.init(annotation: annotation, reuseIdentifier: MapPin.reuseIdentifier)
canShowCallout = false;
}
override func setSelected(selected: Bool, animated: Bool) {
let calloutViewAdded = calloutView?.superview != nil
if (selected || !selected && hitOutside) {
super.setSelected(selected, animated: animated)
}
self.superview?.bringSubviewToFront(self)
if (calloutView == nil) {
calloutView = MapPinCallout()
}
if (self.selected && !calloutViewAdded) {
addSubview(calloutView!)
}
if (!self.selected) {
calloutView?.removeFromSuperview()
}
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
var hitView = super.hitTest(point, withEvent: event)
if let callout = calloutView {
if (hitView == nil && self.selected) {
hitView = callout.hitTest(point, withEvent: event)
}
}
hitOutside = hitView == nil
return hitView;
}
}
class MapPinCallout: UIView {
override func hitTest(var point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let viewPoint = superview?.convertPoint(point, toView: self) ?? point
let isInsideView = pointInside(viewPoint, withEvent: event)
var view = super.hitTest(viewPoint, withEvent: event)
return view
}
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
return CGRectContainsPoint(bounds, point)
}
}
if calloutState == .Expanded && CGRectContainsPoint(tableView.frame, viewPoint) {
view = tableView.hitTest(convertPoint(viewPoint, toView: tableView), withEvent: event)
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if let mapPin = view as? MapPin {
updatePinPosition(mapPin)
}
}
func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!) {
if let mapPin = view as? MapPin {
if mapPin.preventDeselection {
mapView.selectAnnotation(view.annotation, animated: false)
}
}
}
func updatePinPosition(pin:MapPin) {
let defaultShift:CGFloat = 50
let pinPosition = CGPointMake(pin.frame.midX, pin.frame.maxY)
let y = pinPosition.y - defaultShift
let controlPoint = CGPointMake(pinPosition.x, y)
let controlPointCoordinate = mapView.convertPoint(controlPoint, toCoordinateFromView: mapView)
mapView.setCenterCoordinate(controlPointCoordinate, animated: true)
}
关于ios8 - 自定义 iOS8 标注气泡 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066623/
我有一条短线 (MKPolyline) 和一个自定义注释类 (MKPointAnnotaion)。现在我的点注释位于多段线的中点。但是,我希望每当触摸多段线上的任何点时都显示标注,类似于路由在 map
我正在使用此自定义 MKAnnotationView http://blog.asolutions.com/2010/09/building-custom-map-annotation-callout
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicates: Custom MKPinAnnotation callout bubble similar to default
为什么这不起作用? - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { if (TRAC
我试图在不触摸引脚的情况下隐藏 AnnotationView,这可能吗?谢谢! for (id currentAnnotation in self.mapView.annotations) {
我正在使用 JQplot 饼图。我需要标 checkout 现在图表之外。我怎样才能做到这一点? 最佳答案 如果您指的是数据标签,则需要将 dataLabelPositionFactor 设置为大于
我的 map 上有 20 个图钉,点击后每个图钉都会显示带有 field 名称的注释,并且右侧有一个标注按钮。我已使用 segue 成功将此按钮链接到单个页面。问题是所有位置都链接到同一页面。我想要
是否可以像在 iPhone 和 Google map 中那样将标注附加到 ItemizedOverlay 项目? 最佳答案 我假设“标注”是指通常出现的信息窗口/“气球提示”?这是代码和示例: htt
我正在使用 Python 2.7 和 PySide(Qt 包装器)开发 GUI 应用程序。 我希望标注小部件 float 在其他小部件上方(类似于工具提示),但不使用标准工具提示框架,该框架基本上为小
我有一个 TableView,用于在点击单元格时显示 MapView 注释标注。 在 iOS 10 中,我可以将 MapView 置于注释的中心,然后使用以下方法显示它的标注: func tableV
我想创建一个自定义 MKAnnotationView 标注,如此图所示。我已经测试了几种解决方案,但它们只允许自定义左/右图像和标题/副标题。谁能给我一些源代码或教程链接吗? 目前我一无所知。请帮忙。
我一直在寻找可能是这个问题的根源,但我看不出出了什么问题。我希望你能在这里帮助我。 我正在尝试在 mapView 中显示注释,图钉被丢弃但无法看到标注,直到我先点击用户位置注释(蓝点)然后返回并点击注
我需要将 xml 数据转换为 Excel 文件。当我写入文件时,我的代码可以正常工作,但是,当我尝试写入输出流时,代码无法正常工作。 我想做的就是使用 Apache POI 类创建一个 excel 文
如何始终在 map View 中显示标注?如果我们点击 map View 标注隐藏并再次点击图钉显示。我不想这样......,我需要始终显示标注,没有隐藏。这个怎么做。请帮我。我正在使用下面的代码来显
我有一个MKPointAnnotation: let ann = MKPointAnnotation() self.ann.coordinate = annLoc self.ann.title = "
我已遵循指南here创建自定义标注 View 。我在自定义类中添加了两行: var dismissesAutomatically: Bool = false var isAnchoredToAnnot
我们正在 Angular.js 中做一个项目,我们从用户体验人员那里获得的模型大量使用交互式弹出窗口/标注。 问题是我似乎无法以干净的方式将这些映射到 Angular 概念上。从概念上讲,它们需要自己
我在显示注释标题时遇到问题,如下图所示。第一张图片很好地表示了值(value);另一方面,一旦值上升到三位数,标题就会显示三个点,如第二张图片所示。我想知道如何解决这个问题。任何想法都将非常受欢迎!非
我正在分析几百万封电子邮件。我的目标是能够分类然后分组。组可以是例如: 交付问题(交付缓慢、发货前处理缓慢、可用性信息不正确等) 客户服务问题(电子邮件回复速度慢、回复不礼貌等) 返回问题(返回请求处
我有一个MKPointAnnotation: let ann = MKPointAnnotation() self.ann.coordinate = annLoc self.ann.title = "
我是一名优秀的程序员,十分优秀!