iOS - 如何设置子类化MKAnnotationView的大小动画?

时间:2015-06-12 05:19:56

标签: ios xcode swift mkmapview mkannotationview

在我的应用程序中,我使用子类化的MKAnnotationView将我自己的“注释”绘制到MapView上。哪个工作正常。

我想要发生的是当我在MapViewController中调用函数“didSelectAnnotationView”时,它会将弹性的Annotation设置为更大的动画。在我的例子中,基本大小是CGSize(26,26),而较大的大小是CGSize(78,78)。

我有一段时间刚刚到达我的代码所在的地方,而且我已经烧尽了。所以它现在的位置,我甚至没有实现任何动画,我只是试图让该死的东西正确地增长到更大的尺寸。当“didSelect”函数被调用并且我调用“myPinView.animateSize(shouldGrow:,fromDrawRect :)”函数来重绘所有内容并且它确实绘制了一个更大的Pin但它将较小的Pin保持在所有内容之下时会发生什么。

非常感谢任何帮助。

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)
    }
  }
}

}

0 个答案:

没有答案