here is what you need to implement that especial animation, imagine that you have two similar rectangle which their proportion is the same then we use the equation of W1/W2 = H1/H2 which we get H2 = (W2/W1)H1 and if you are not interested on math section take a look at the code which is basically two function for zoomIn animation and zoomOut to get back to the absolute coordinate system.
let zoomImageView = UIImageView()
let blackBackgroundView = UIView()
let navBarCoverView = UIView()// to hide navigation bar
let tabBarCoverView = UIView() // to hide tab bar
var statusImageView: UIImageView?
func animateImageView(statusImageView: UIImageView) {
self.statusImageView = statusImageView
//to get absolute coordinate system
if let startingFrame = statusImageView.superview?.convertRect(statusImageView.frame, toView: nil) {
statusImageView.alpha = 0
blackBackgroundView.frame = self.view.frame
blackBackgroundView.backgroundColor = UIColor.blackColor()
blackBackgroundView.alpha = 0
view.addSubview(blackBackgroundView)
navBarCoverView.frame = CGRectMake(0, 0, 1000, 20 + 44)
navBarCoverView.backgroundColor = UIColor.blackColor()
navBarCoverView.alpha = 0
if let keyWindow = UIApplication.sharedApplication().keyWindow {
keyWindow.addSubview(navBarCoverView)
tabBarCoverView.frame = CGRectMake(0, keyWindow.frame.height - 49, 1000, 49)
tabBarCoverView.backgroundColor = UIColor.blackColor()
tabBarCoverView.alpha = 0
keyWindow.addSubview(tabBarCoverView)
}
zoomImageView.backgroundColor = UIColor.redColor()
zoomImageView.frame = startingFrame
zoomImageView.userInteractionEnabled = true
zoomImageView.image = statusImageView.image
zoomImageView.contentMode = .ScaleAspectFill
zoomImageView.clipsToBounds = true
view.addSubview(zoomImageView)
zoomImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "zoomOut"))
UIView.animateWithDuration(0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .CurveEaseOut, animations: { () -> Void in
let height = (self.view.frame.width / startingFrame.width) * startingFrame.height
let y = self.view.frame.height / 2 - height / 2
self.zoomImageView.frame = CGRectMake(0, y, self.view.frame.width, height)
self.blackBackgroundView.alpha = 1
self.navBarCoverView.alpha = 1
self.tabBarCoverView.alpha = 1
}, completion: nil)
}
}
func zoomOut() {
if let startingFrame = statusImageView!.superview?.convertRect(statusImageView!.frame, toView: nil) {
UIView.animateWithDuration(0.75, animations: { () -> Void in
self.zoomImageView.frame = startingFrame
self.blackBackgroundView.alpha = 0
self.navBarCoverView.alpha = 0
self.tabBarCoverView.alpha = 0
}, completion: { (didComplete) -> Void in
self.zoomImageView.removeFromSuperview()
self.blackBackgroundView.removeFromSuperview()
self.navBarCoverView.removeFromSuperview()
self.tabBarCoverView.removeFromSuperview()
self.statusImageView?.alpha = 1
})
}
}
}