2016-06-27 63 views
0

我有一個目標圖像,我試圖使用動畫來裁剪外環而不調整其大小。我正在使用UIBelzierPath來做到這一點,我有一些原因不能將它直接放在我的圖像上,因爲它只是動畫像右下角那樣,直接應用於imageView。我究竟做錯了什麼?我與動畫鬥爭,所以任何幫助都會很棒。不以imageView爲中心的動畫

@IBOutlet weak var image: UIImageView! 

@IBAction func animate(sender: AnyObject) { 
    animateCollapse() 
} 

private func animateCollapse() { 
    let view = image 
    let circleMaskPathInitial = UIBezierPath(ovalInRect: view.frame) 
    let circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(view.frame, 10 , 10)) 
    performAnimation(circleMaskPathInitial, finalPath: circleMaskPathFinal) 
} 

private func performAnimation(initialPath: UIBezierPath, finalPath: UIBezierPath) { 

    let maskLayer = CAShapeLayer() 
    maskLayer.path = finalPath.CGPath 
    image.layer.mask = maskLayer 

    let maskLayerAnimation = CABasicAnimation(keyPath: "path") 
    maskLayerAnimation.fromValue = initialPath.CGPath 
    maskLayerAnimation.toValue = finalPath.CGPath 
    maskLayerAnimation.duration = 1.0 
    maskLayerAnimation.delegate = self 
    maskLayer.addAnimation(maskLayerAnimation, forKey: "path") 
} 

圖像:動畫後enter image description here
圖像:enter image description here

回答

2

嘗試改變let circleMaskPathInitial = UIBezierPath(ovalInRect: view.frame)let circleMaskPathInitial = UIBezierPath(ovalInRect: view.bounds)。我的猜測是,你真的想在圖像視圖的(0,0)處使用矩形的原點,但是它的原點不是(0,0)。

+0

也必須更新circleMaskPathFinal帶邊界 - > UIBezierPath(ovalInRect:CGRectInset(view.bounds,10,10)) –

+0

謝謝基思 –