我在我的图片视图中添加了一个点按手势识别器动作。一旦图像被点击,它就会扩展到全屏,然后再次点击后会关闭全屏。如何在图像展开后添加缩放以缩放图像。这是我将图像全屏显示的代码。
//expand image
let newImageView: UIImageView!
@IBAction func imageTapped(_ sender: UITapGestureRecognizer)
{
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame)
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
答案 0 :(得分:0)
如果你想缩放imageView,你应该在UIScrollView上添加imageView并添加下面的方法:
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// The zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the
// imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible,
// the size of the rect grows.
zoomRect.size.height = scrollView.frame.size.height / scale;
zoomRect.size.width = scrollView.frame.size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
答案 1 :(得分:0)
如果您想要显示完整图片,请使用scrollview
代替imageView
let newImageView: UIImageView!
@IBAction func imageTapped(_ sender: UITapGestureRecognizer)
{
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame);
newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
并记住viewForZooming
委托功能
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}