2016-04-25 16 views
12

Ich habe eine einfache UIBezierPath Animation mit Swift gemacht. Dieser Pfad besteht darin, ein abgerundetes Rechteck mit einem farbigen Rahmen zu erstellen. Die Animation muss die Zeichnung der farbigen Umrandung sein. Dazu habe ich eine CAShapeLayer mit einem UIBezierPath(roundedRect:, cornerRadius:) erstelltSwift: UIBezierPath Stroke Animation von Center

let layer = CAShapeLayer() 
var viewPrueba = UIView() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    viewPrueba = UIView(frame: CGRectMake(self.view.frame.width/2-100, self.view.frame.height/2 - 100, 200, 200)) 
    self.view.addSubview(viewPrueba) 
    let path = UIBezierPath(roundedRect: CGRectMake(0, 0, 200, 200), cornerRadius: 40.0) 
    layer.path = path.CGPath 
    layer.fillColor = UIColor.clearColor().CGColor 
    layer.strokeColor = UIColor.blueColor().CGColor 
    layer.strokeStart = 0.0 
    layer.strokeEnd = 0.0 
    layer.lineWidth = 4.0 
    layer.lineJoin = kCALineJoinRound 
    viewPrueba.layer.addSublayer(layer) 
    let tapGR = UITapGestureRecognizer(target: self, action: #selector(ViewController.anim)) 
    self.view.addGestureRecognizer(tapGR) 
} 

func anim() { 
    let anim1 = CABasicAnimation(keyPath: "strokeEnd") 
    anim1.fromValue   = 0.0 
    anim1.toValue   = 1.0 
    anim1.duration   = 4.0 
    anim1.repeatCount  = 0 
    anim1.autoreverses  = false 
    anim1.removedOnCompletion = false 
    anim1.additive = true 
    anim1.fillMode = kCAFillModeForwards 
    self.layer.addAnimation(anim1, forKey: "strokeEnd") 
}` 

es gut funktioniert. Das einzige Problem ist, dass die Animation von der oberen linken Ecke des Quadrats und nicht von der oberen Mitte beginnt. Wie kann ich das machen?

Das einzige, was ich gefunden habe, um dies zu erreichen, ist, indem ich es mit einem Kreis und nicht einem Rechteck mache, was nicht das ist, was wir wollen.

This is where the animation starts

Dank

Antwort

12

CoreAnimate als die gleiche Reihenfolge animiert, wie denen die UIBezierPath gezogen.
Das System Methode

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;  

Rückkehr ein UIBezierPath, die sich von der oberen linken Ecke gezogen wurde, so die Animation von der oberen linken gestartet.
Aber Sie können Ihre eigene UIBezierPath gezogen Form oben in der Mitte erstellen:

func centerStartBezierPath(frame:CGRect,cornerRadius:CGFloat) -> UIBezierPath { 
    let path = UIBezierPath() 
    path.moveToPoint(CGPointMake(frame.width/2.0, 0)) 
    path.addLineToPoint(CGPointMake(frame.width-cornerRadius, 0)) 
    path.addArcWithCenter(CGPointMake(frame.width-cornerRadius, cornerRadius), 
          radius: cornerRadius, 
          startAngle: CGFloat(-M_PI/2), 
          endAngle: 0, 
          clockwise: true) 
    path.addLineToPoint(CGPointMake(frame.width, frame.height-cornerRadius)) 
    path.addArcWithCenter(CGPointMake(frame.width-cornerRadius, frame.height-cornerRadius), 
          radius: cornerRadius, 
          startAngle: 0, 
          endAngle: CGFloat(M_PI/2), 
          clockwise: true) 
    path.addLineToPoint(CGPointMake(cornerRadius, frame.height)) 
    path.addArcWithCenter(CGPointMake(cornerRadius, frame.height-cornerRadius), 
          radius: cornerRadius, 
          startAngle: CGFloat(M_PI/2), 
          endAngle: CGFloat(M_PI), 
          clockwise: true) 
    path.addLineToPoint(CGPointMake(0, cornerRadius)) 
    path.addArcWithCenter(CGPointMake(cornerRadius, cornerRadius), 
          radius: cornerRadius, 
          startAngle: CGFloat(M_PI), 
          endAngle: CGFloat(M_PI*3/2), 
          clockwise: true) 
    path.closePath() 

    path.applyTransform(CGAffineTransformMakeTranslation(frame.origin.x, frame.origin.y)) 

    return path; 
}  

Und es funktioniert wie folgt: top-center start animation
Sie können auch den Code ändern, und starten Sie von jedem Punkt, den Sie wollen.

+0

Funktioniert wie Charme! Vielen Dank! :) – rulilg