通过测试发现老版通过present控制器出现问题
问题描述(主要在iOS11以下) 点击present控制器后,其实控制器是出来的 只是层次结构出现了问题 通过下2图 可以看出来
后面直接改成弹出view
调用方法(一句代码实现)
HWNewImageBrowse.showView(imageView:imageView)
实现原理
import UIKit
///屏幕高度 && The screen height
private let HWNewImageBrowseScreenHeight = UIScreen.main.bounds.size.height
class HWNewImageBrowse: UIView {
private var imageView = UIImageView.init()
private var _image : UIImage?
private var fromRect: CGRect? // 用于记录开始位置
var image : UIImage? {
set {
_image = newValue
if newValue == nil {return}
addSubview(imageView)
imageView.frame = getFullScreenImageRectWithImage(image: newValue!)
imageView.image = newValue
}
get {
return _image
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.black
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// 快速显示 imageView 必传
class func showView(imageView:UIImageView) {
let window = UIApplication.shared.keyWindow
let view = HWNewImageBrowse.init(frame: UIScreen.main.bounds)
/// 获取原图片在屏幕上的位置
let oldrect = imageView.convert(imageView.bounds, to: window!)
// 获取大图尺寸
let bigrect = getFullScreenImageRectWithImage(image: imageView.image!)
var newRect:CGRect?
if bigrect.size.width >= bigrect.size.height { // 比较宽 高度为标准
let newW = oldrect.size.height*bigrect.size.width/bigrect.size.height
newRect = CGRect.init(x: oldrect.origin.x-(newW-oldrect.size.width)/2, y: oldrect.origin.y, width: newW, height: oldrect.size.height)
} else { // 比较高 宽度为标准
let newH = oldrect.size.width*bigrect.size.height/bigrect.size.width
newRect = CGRect.init(x: oldrect.origin.x, y: oldrect.origin.y - (newH - oldrect.size.height)/2, width: oldrect.size.width, height: newH)
}
// 记录新图片位置
view.fromRect = newRect
view.isHidden = true
view.image = imageView.image
window?.addSubview(view)
// 添加黑色背景
let background = UIView(frame: UIScreen.main.bounds)
background.backgroundColor = UIColor.black
background.alpha = 0.0
window?.addSubview(background)
// 添加过渡图片
let tempView = UIImageView.init()
window?.addSubview(tempView)
tempView.image = imageView.image
tempView.frame = newRect!//imageView.convert(imageView.bounds, to: window!)
let heightS = (imageView.bounds.size.height)/(imageView.bounds.size.width)*UIScreen.main.bounds.size.width
UIView.animate(withDuration: 0.3, animations: {
background.alpha = 1
tempView.frame = bigrect
if heightS < HWNewImageBrowseScreenHeight {
tempView.center = (window?.center)!
}
}, completion: { (callBack) in
background.removeFromSuperview()
tempView.removeFromSuperview()
view.isHidden = false
})
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let window = UIApplication.shared.keyWindow
self.isHidden = false
self.alpha = 0
let background = UIView(frame: UIScreen.main.bounds)
background.backgroundColor = UIColor.black
background.alpha = 1
window?.addSubview(background)
let tempView = UIImageView.init()
window?.addSubview(tempView)
tempView.image = imageView.image
tempView.frame = getFullScreenImageRectWithImage(image: self.imageView.image!)
UIView.animate(withDuration: 0.3, animations: { [weak self] in
background.alpha = 0
tempView.frame = self!.fromRect!
}, completion: { [weak self](callBack) in
background.removeFromSuperview()
tempView.removeFromSuperview()
self!.removeFromSuperview()
})
}
}
/// 获取图片尺寸
private func getFullScreenImageRectWithImage(image:UIImage) -> CGRect {
let size: CGSize = image.size
let scaleX: CGFloat = UIScreen.main.bounds.size.width / size.width
let scaleY: CGFloat = UIScreen.main.bounds.size.height / size.height
if scaleX > scaleY {
let width: CGFloat = size.width * scaleY
return CGRect(x: UIScreen.main.bounds.size.width / 2.0 - width / 2.0, y: 0.0, width: width, height: UIScreen.main.bounds.size.height)
} else {
let height: CGFloat = size.height * scaleX
return CGRect(x: 0, y: UIScreen.main.bounds.size.height / 2.0 - height / 2.0, width: UIScreen.main.bounds.size.width, height: height)
}
}