iOS-如何设置子类MKAnnotationView大小的动画?
在我的应用程序中,我使用子类MKAnnotationView在地图视图上绘制我自己的“注释”。这很好用 我希望在MapViewController中调用函数“didSelectAnnotationView”时,它将使用SpringNess将注释设置为更大尺寸的动画。在我的例子中,基本大小是CGSize(26,26),较大的大小是CGSize(78,78) 我已经经历了一个地狱般的时间,刚刚到达我的代码所在的位置,至少可以说我已经筋疲力尽了。所以,就目前的情况而言,我甚至没有实现任何动画,我只是想让这该死的东西正确地增长到更大的尺寸。当调用“didSelect”函数时,我调用“myPinView.animateSize(shouldGrow:,fromDrawRect:)”函数重新绘制所有内容,它确实绘制了一个较大的管脚,但它将较小的管脚保留在所有内容下面 任何帮助都将不胜感激iOS-如何设置子类MKAnnotationView大小的动画?,ios,xcode,swift,mkmapview,mkannotationview,Ios,Xcode,Swift,Mkmapview,Mkannotationview,在我的应用程序中,我使用子类MKAnnotationView在地图视图上绘制我自己的“注释”。这很好用 我希望在MapViewController中调用函数“didSelectAnnotationView”时,它将使用SpringNess将注释设置为更大尺寸的动画。在我的例子中,基本大小是CGSize(26,26),较大的大小是CGSize(78,78) 我已经经历了一个地狱般的时间,刚刚到达我的代码所在的位置,至少可以说我已经筋疲力尽了。所以,就目前的情况而言,我甚至没有实现任何动画,我只是想
class myPinView: MKAnnotationView {
var outlineColor: UIColor = UIColor.orangeColor()
var textSize: CGFloat = CGFloat(9.0)
var lastTextString: String = ""
var textLabel: UILabel = UILabel()
var textString: String = "RG" {
didSet {
lastTextString = oldValue
}
}
private var diameter: CGFloat = 0
private var arcWidth: CGFloat = 0
private var arcCenter: CGPoint = CGPointZero
private var startAngle: CGFloat = 3 * pi / 4
private var endAngle: CGFloat = pi / 4
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
drawPin(false, fromDrawRect: true)
}
func animateSize(shouldGrow: Bool, text: String) {
if shouldGrow {
textSize = 18.0
textString = text
} else {
textSize = 9.0
textString = lastTextString
}
drawPin(shouldGrow, fromDrawRect: false)
}
func drawPin(shouldGrow: Bool, fromDrawRect: Bool) {
var b = bounds
var boundsForDrawing: CGRect
if shouldGrow {
boundsForDrawing = CGRect(x: b.origin.x, y: b.origin.y, width: b.size.width + 52, height: b.size.height + 52)
} else {
boundsForDrawing = CGRect(x: b.origin.x, y: b.origin.y, width: 26, height: 26)
}
diameter = min(boundsForDrawing.width, boundsForDrawing.height)
arcWidth = diameter * CGFloat(0.7)
arcCenter = CGPoint(x: boundsForDrawing.width/2, y: boundsForDrawing.height/2 - (diameter/2 * CGFloat(0.25)))
var startXoffset = arcCenter.x - (sin(startAngle) * arcWidth/2)
var startYoffset = arcCenter.y - (cos(startAngle) * arcWidth/2)
var path = UIBezierPath(
arcCenter: arcCenter,
radius: arcWidth/2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: true)
path.addQuadCurveToPoint(CGPointMake(arcCenter.x, boundsForDrawing.height/2 + diameter/2),
controlPoint: CGPointMake(arcCenter.x, arcCenter.y + (arcWidth * CGFloat(0.7))))
path.addQuadCurveToPoint(CGPointMake(startXoffset, startYoffset),
controlPoint: CGPointMake(arcCenter.x, arcCenter.y + (arcWidth * CGFloat(0.7))))
if fromDrawRect {
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSizeMake(0, 5.0)
layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
}
var innerPath = UIBezierPath(
arcCenter: arcCenter,
radius: (arcWidth/2 * CGFloat(0.8)),
startAngle: 0,
endAngle: 2 * pi,
clockwise: true)
innerPath.lineWidth = CGFloat(1.0)
var maskOuterLayer = CAShapeLayer()
maskOuterLayer.path = path.CGPath
maskOuterLayer.fillColor = outlineColor.CGColor
var maskInnerLayer = CAShapeLayer()
maskInnerLayer.path = innerPath.CGPath
maskInnerLayer.fillColor = UIColor.clearColor().CGColor
maskInnerLayer.strokeColor = UIColor.blackColor().CGColor
textLabel.font = UIFont(name: "MarkerFelt-Thin", size: textSize)
var textPointSize = textLabel.font.pointSize
var lblStartPoint = CGPointMake(arcCenter.x - (sin(startAngle) * (arcWidth/2 * CGFloat(0.8))),
arcCenter.y - (cos(startAngle) * (arcWidth/2 * CGFloat(0.8))))
var lblSize = CGSizeMake((sin(startAngle) * (arcWidth * CGFloat(0.8))),
(cos(startAngle) * (arcWidth * CGFloat(0.8))))
textLabel.frame = CGRect(origin: lblStartPoint, size: lblSize)
textLabel.center = arcCenter
textLabel.text = textString
textLabel.textColor = UIColor.blackColor()
textLabel.backgroundColor = UIColor.clearColor()
textLabel.adjustsFontSizeToFitWidth = true
textLabel.textAlignment = NSTextAlignment.Center
maskInnerLayer.addSublayer(textLabel.layer)
maskOuterLayer.addSublayer(maskInnerLayer)
if layer.sublayers?.count > 0 {
layer.replaceSublayer(layer.sublayers[0] as! CAShapeLayer, with: maskOuterLayer)
} else {
layer.addSublayer(maskOuterLayer)
}
if shouldGrow {
bounds.size = CGSize(width: 78, height: 78)
} else {
if !fromDrawRect {
bounds.size = CGSize(width: 26, height: 26)
}
}
}
}