func didTap() {
print("浏览照片")
let image = self.chatImageView.image
let window = self.viewController().view
let backgroundView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: HEIGHT))
oldFrame = self.chatImageView.convert(self.chatImageView.bounds, to: window)
backgroundView.backgroundColor = UIColor.black
backgroundView.alpha = 0.5
let imageView = UIImageView.init(frame: oldFrame)
imageView.image = image
imageView.tag = 1
backgroundView.addSubview(imageView)
window?.addSubview(backgroundView)
//点击图片缩小的手势
let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.hideImage(tap:)))
tap.numberOfTapsRequired = 1
let longTap = UILongPressGestureRecognizer.init(target: self, action: #selector(self.alertShow))
backgroundView.addGestureRecognizer(tap)
backgroundView.addGestureRecognizer(longTap)
UIView.animate(withDuration: 0.3) {
imageView.frame = CGRect.init(x: 0, y: (HEIGHT - (image?.size.height)! * WIDTH/(image?.size.width)!)/2, width: WIDTH, height: (image?.size.height)! * WIDTH/(image?.size.width)!)
backgroundView.alpha = 1
}
}
func hideImage(tap: UITapGestureRecognizer) {
let backgroundView = tap.view
let imageView = tap.view?.viewWithTag(1) as! UIImageView
UIView.animate(withDuration: 0.3, animations: {
imageView.frame = self.oldFrame
backgroundView?.alpha = 0
}) { (finished) in
backgroundView?.removeFromSuperview()
}
}
func alertShow() {
let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
let action1 = UIAlertAction.init(title: "下载图片", style: UIAlertActionStyle.default) { (action) in
}
let action2 = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel) { (action) in
}
alert.addAction(action1)
alert.addAction(action2)
self.viewController().present(alert, animated: true, completion: nil)
}
//获取当前视图所在控制器
func viewController () -> (UIViewController){
var next:UIResponder?
next = self.next!
repeat {
if ((next as?UIViewController) != nil) {
return (next as! UIViewController)
}else {
next = next?.next
}
} while next != nil
return UIViewController()
}