自定义iOS8标注泡沫(Swift)

自定义iOS8标注泡沫(Swift),第1张

概述我想定制iOS8 MapView标注泡泡,当您点击MKAnnotationView时可以看到它.默认气泡是有限制的(只有标题,副标题和2附件视图),所以我很难找到一个替代解决方案.这里有两种可能的方法和我面临的相对问题: 问题1)创建一个自定义的呼叫泡泡 挖掘Apple documentation我发现这个: When you use a custom view instead of a stan 我想定制iOS8 MapVIEw标注泡泡,当您点击MKAnnotationVIEw时可以看到它.默认气泡是有限制的(只有标题,副标题和2附件视图),所以我很难找到一个替代解决方案.这里有两种可能的方法和我面临的相对问题:

问题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];    }}

现在,当我尝试将此Objective-C代码转换为Swift时,我找不到此属性:

NSVIEw *calloutVIEw = self.calloutVIEwController.vIEw;

如何访问标注气泡视图?

问题2)修改默认呼叫泡泡

如前所述,显示的默认标注包括标题,副标题和2个附件视图.我注意到我不能改变字符串的字体风格或泡沫的颜色.另外如果我的标题有超过24个字符我的附件视图定位被搞砸了.
如何避免这个问题?

解决方法 calloutVIEwController是处理事件的自定义标注视图的一部分.你不会在MapKit或其他地方找到它.
苹果的指示是好的.要创建自己的标注,您应该遵循以下步骤:
1. Create custom MKAnnotationVIEw or MAPInAnnotationVIEw2. OverrIDe setSelected and hitTest methods in your annotation3. Create your own callout vIEw4. OverrIDe hitTest and pointInsIDe in you callout vIEw5. 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)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1113350.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存