Krisiacik / ImageViewer

An image viewer à la Twitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Displacement view center bug on rotation presentation

rafaelcr opened this issue · comments

I'm presenting ImageViewer on rotation so that the user can see a picture in full screen. When I do so, however, the center that the displacement view animates to is completely wrong. Here's an example:

bug

Notice how the center is fixed once the fetchImage block is called and the view is layed out again. The same thing happens if I rotate the device and click on the image to present.

I was able to patch it in ItemBaseController's presentItem(alongsideAnimation:completion:) as such, but I'm sure it's a hack and not a proper fix:

UIView.animate(withDuration: displacementDuration, delay: 0, usingSpringWithDamping: displacementSpringBounce, initialSpringVelocity: 1, options: .curveEaseIn, animations: { [weak self] in

                    if UIApplication.isPortraitOnly == true {
                        animatedImageView.transform = CGAffineTransform.identity
                    }
                    /// Animate it into the center (with optionally rotating) - that basically includes changing the size and position

                    animatedImageView.bounds.size = self?.displacementTargetSize(forSize: image.size) ?? image.size
// Patch starts ------------
                  if UIApplication.isPortraitOnly, UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
                    animatedImageView.center = CGPoint(x: UIScreen.main.bounds.height / 2, y: UIScreen.main.bounds.width / 2)
                  } else {
                    animatedImageView.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
                  }
// Patch ends ------------
                    }, completion: { [weak self] _ in

                        self?.itemView.isHidden = false
                        displacedView.isHidden = false
                        animatedImageView.removeFromSuperview()

                        self?.isAnimating = false
                        completion()
                    })
            }

I'm using v5.0

@rafaelcr thanks, your solution works good!