动画不以imageView(iOS)为中心

时间:2016-06-27 18:20:45

标签: ios animation

我有一个目标的图像,我正在尝试使用动画来裁剪外环而不调整其大小。我正在使用UIBelzierPath来执行此操作,我有些原因无法将其直接放在我的图像上,因为它只会动画到右下角,就像直接应用于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

1 个答案:

答案 0 :(得分:2)

尝试将let circleMaskPathInitial = UIBezierPath(ovalInRect: view.frame)更改为let circleMaskPathInitial = UIBezierPath(ovalInRect: view.bounds)。我的猜测是你真的希望矩形的原点位于图像视图的(0,0)处,但它的帧的原点不是(0,0)。