除了我无法放大图像视图外,一切都很完美。
var zoomingImageView = UIImageView()
var startingFrame : CGRect?
var blackBackground : UIScrollView?
var startingImageView: UIImageView?
这是我在其中设置scrollview和imageview的地方
func performZoomingForStartImageView(imageView: UIImageView){
if let keyWindow = UIApplication.shared.keyWindow {
blackBackground = UIScrollView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: self.view.frame.size.height))
blackBackground?.delegate = self
blackBackground?.maximumZoomScale = 4.0
blackBackground?.minimumZoomScale = 1.0
blackBackground?.isScrollEnabled = true
blackBackground?.backgroundColor = .black
blackBackground?.alpha = 0.0
blackBackground?.isUserInteractionEnabled = true
blackBackground?.contentSize = CGSize(width: 0, height: self.view.frame.size.height)
keyWindow.addSubview(blackBackground!)
self.startingImageView = imageView
self.startingImageView?.isHidden = true
startingFrame = imageView.superview?.convert(imageView.frame, to: nil)
zoomingImageView = UIImageView(frame: startingFrame!)
zoomingImageView.image = startingImageView?.image
zoomingImageView.contentMode = .scaleAspectFit
zoomingImageView.isUserInteractionEnabled = true
zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleZoomOut)))
zoomingImageView.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.saveImage)))
blackBackground?.addSubview(zoomingImageView)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseOut,.allowUserInteraction], animations: {
self.blackBackground?.alpha = 1.0
self.inputContainerView.alpha = 0
//let height = startingFrame!.height / startingFrame!.height * keyWindow.frame.widthimagedShown
self.zoomingImageView.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.zoomingImageView.center = keyWindow.center
}, completion: nil)
}
}
这应该允许缩放,但打印永远不会被调用,我已经设置了委托
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
print("hi")
return zoomingImageView
}
答案 0 :(得分:2)
对于Swift 3,方法签名已更改。
斯威夫特3:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}