大佬教程收集整理的这篇文章主要介绍了自定义iOS8标注泡沫(Swift),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
挖掘Apple documentation我发现这个:
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 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]; } }
现在,当我尝试将此Objective-C代码转换为Swift时,我找不到此属性:
NSView *calloutView = self.calloutViewController.view;
如何访问标注气泡视图?
如前所述,显示的默认标注包括标题,副标题和2个附件视图.我注意到我不能改变字符串的字体风格或泡沫的颜色.另外如果我的标题有超过24个字符我的附件视图定位被搞砸了.
如何避免这个问题?
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) } }
如果您需要其他东西,但按钮可以在标注中添加代码,以在hitTest返回视图之前处理特定视图中的触摸
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标注泡沫(Swift)全部内容,希望文章能够帮你解决自定义iOS8标注泡沫(Swift)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。