3

I'm facing a big issue.. And I just don't know why it does not work correctly. I'm making a gallery application and want to have smooth transition between gallery and fullscreen image.

I though my animation worked perfectly.. but I captured a picture with different orientation and my animation doesn't work anymore. Do you have any idea why my animation is dependent to the orientation of the picture ?

here is my working animation.

enter image description here

Here is my broken animation

enter image description here

As you can see the height and the width are the same, and this is not a ratio problem.

here is my animation

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

}

All my frames are good in either of way, I checked them 4 times, my begin and ending frames are good.

Frames

Is there something I should know about pictures, orientation or animation ?

Sacha.R
  • 394
  • 2
  • 17
  • Did you check to see if the viewToAnimate.frame is not a relative frame to the collection cell itself? because then having an X of 214 makes it go out of bounds. What are the parameters of the frame for the working animation and what is the value of "originFrame"? – unkgd Feb 01 '18 at 19:05
  • Try setting `viewToAnimate.frame = fromView.superview!.convert(fromView.frame, to: transitionContext.containerView)` – Brandon Feb 04 '18 at 00:37
  • How do you set `originFrame` in this statement `let viewToAnimate = UIImageView(frame: originFrame)`? – LGP Feb 05 '18 at 08:57
  • see this http://blog.jtribe.com.au/title/ – Prashant Tukadiya Feb 07 '18 at 09:24

1 Answers1

2

UIImage orientations is one big messy bug. The easiest way is to normalize all UIImage instances in imageViews to one orientation. How it's done if well described in this answer

mas'an
  • 170
  • 6
  • Actually, change the orientation does fix my problem, but it's not a fix, it's a bypass.. But at least I know that the problem comes from the orientation – Sacha.R Feb 14 '18 at 08:19