2016-06-27 23 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

谢谢基思 –