2016-04-25 77 views
12

我一直在做一個簡單的UIBezierPath動畫與斯威夫特UIBezierPath中風動畫。該路徑包括創建帶有彩色邊框的圓角矩形。動畫必須是彩色邊框的圖形。要做到這一點,我創建了UIBezierPath(roundedRect:, cornerRadius:)斯威夫特:從中心

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

它運作良好,一個CAShapeLayer。唯一的問題是,動畫從頂部中心廣場的左上部分,而不是開始。我怎樣才能做到這一點?

我在爲了實現這一發現的唯一的事情就是用一個圓圈,而不是一個矩形,這不是我們想要做的事情吧。

This is where the animation starts

由於

回答

12

CoreAnimate動畫作爲所述的UIBezierPath繪製的順序相同。
系統方法

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

回報這是從左上方,讓您的動畫從左上角開始繪製的UIBezierPath。
但你可以創建自己的UIBezierPath繪製表格頂部中心:

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

而且它的工作原理是這樣的:top-center start animation
您也可以更改代碼,以及任何你想要的點開始。

+0

作品般的魅力!謝謝! :) – rulilg