为什么快速动画会移动我的视线?

时间:2018-01-18 15:46:50

标签: ios swift

我在swift中有一些自定义动画的小问题。这是从图库到全屏图像的过渡。

我的动画效果很好,但是当我点击图像时,它会在开始动画之前改变它的位置。

实际上问题是在它进入UIView.animate{}方法时开始,它稍微向右移动。

这是我的自定义动画:

class PresentingAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    private let indexPath: IndexPath
    private let originFrame: CGRect
    private let duration: TimeInterval = 4.5

    init(pageIndex: Int, originFrame: CGRect) {
        self.indexPath = IndexPath(item: pageIndex, section: 0)
        self.originFrame = originFrame
        super.init()
    } 

    func transitionDuration(using transitionContext: 
UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        guard   let toView = transitionContext.view(forKey: .to),
                let fromVC = transitionContext.viewController(forKey: .from)?.childViewControllers.first as? NavigationGalleryViewController,
                let fromView = fromVC.collectionView?.cellForItem(at: indexPath) as? InstallationViewCell else {
                transitionContext.completeTransition(true)
                return
        }

        let finalFrame = toView.frame

        let viewToAnimate = UIImageView(frame: originFrame)
        viewToAnimate.image = fromView.imageView.image
        viewToAnimate.contentMode = .scaleAspectFill
        viewToAnimate.clipsToBounds = true
        fromView.imageView.isHidden = true

        let containerView = transitionContext.containerView
        containerView.addSubview(toView)
        containerView.addSubview(viewToAnimate)

        toView.isHidden = true

        // Determine the final image height based on final frame width and image aspect ratio
        let imageAspectRatio = viewToAnimate.image!.size.width / viewToAnimate.image!.size.height
        var finalImageheight = finalFrame.width / imageAspectRatio

        if (finalImageheight > UIScreen.main.bounds.height) {
            finalImageheight = UIScreen.main.bounds.height
        }
        // Animate size and position
        UIView.animate(withDuration: duration, animations: {
            viewToAnimate.frame.size.width = finalFrame.width
            viewToAnimate.frame.size.height = finalImageheight
            viewToAnimate.center = CGPoint(x: finalFrame.midX, y: finalFrame.midY)
        }, completion:{ _ in
                toView.isHidden = false
                fromView.imageView.isHidden = false
                viewToAnimate.removeFromSuperview()
                transitionContext.completeTransition(true)
            })
        }
}

这是我的错误动画

animation

以下是我在UIVIew.animate时的样子。如您所见,位于另一个之前的图像位于正确放置的UITransitionView中。 Visuals

这是视图的精确框架

Frames

但是当我的代码进入动画时,我的图像会在右边3厘米处重复0宽度,我的动画从此开始。

有什么我不了解动画吗?我想我做得对,因为我有我想要的效果,但我不知道为什么我的图像突然被移动到另一个地方(所以动画看起来像垃圾)

0 个答案:

没有答案