Skip to content

Instantly share code, notes, and snippets.

@williamhqs
Created January 16, 2016 07:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamhqs/13115ac665266750bd38 to your computer and use it in GitHub Desktop.
Save williamhqs/13115ac665266750bd38 to your computer and use it in GitHub Desktop.
//
// GSSimpleImageView.swift
// GSSimpleImage
//
// Created by 胡秋实 on 16/1/2016.
// Copyright © 2016 CocoaPods. All rights reserved.
//
import UIKit
class GSSimpleImageView: UIImageView {
var bgView: UIView!
var animated: Bool = true
//MARK: Life cycle
override func drawRect(rect: CGRect) {
super.drawRect(rect)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addTapGesture()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addTapGesture()
fatalError("init(coder:) has not been implemented")
}
//MARK: Private methods
func addTapGesture() {
let tap = UITapGestureRecognizer(target: self, action: "fullScreenMe")
self.addGestureRecognizer(tap)
self.userInteractionEnabled = true
}
//MARK: Actions of Gestures
func exitFullScreen () {
bgView.removeFromSuperview()
}
func fullScreenMe() {
if let window = UIApplication.sharedApplication().delegate?.window {
bgView = UIView(frame: UIScreen.mainScreen().bounds)
bgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "exitFullScreen"))
bgView.backgroundColor = UIColor.blackColor()
let imageV = UIImageView(image: self.image)
imageV.frame = bgView.frame
imageV.contentMode = .ScaleAspectFit
self.bgView.addSubview(imageV)
window?.addSubview(bgView)
if animated {
var sx:CGFloat=0, sy:CGFloat=0
if self.frame.size.width > self.frame.size.height {
sx = self.frame.size.width/imageV.frame.size.width
imageV.transform = CGAffineTransformMakeScale(sx, sx)
}else{
sy = self.frame.size.height/imageV.frame.size.height
imageV.transform = CGAffineTransformMakeScale(sy, sy)
}
UIView.animateWithDuration(0.5, animations: { () -> Void in
imageV.transform = CGAffineTransformMakeScale(1, 1)
})
}
}
}
}
@luisgizirian
Copy link

Neat work William!
How do you foresee considering orientation rotation scenarios? (i.e. a UITableViewCell containing a UIImageView as GSSimpleImageView thru Storyboard setup. Everything works fine, but when the device gets rotated, once in FullScreen mode, the component needs to rearrange).

UPDATE: Delegation with protocols looks like a right option.

@vin-the-dev
Copy link

Thanks a lot for the gist, works great,
Just a quick question, now the image pops up from the centre of the screen, can we make it coming from the frame of the image we tapped?

I have seen this in different apps, like Facebook, whatsapp etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment