在扩展图像上捏合手势识别器
pinch gesture recognizer on expanded image
我已将点击手势识别器操作添加到我的图像视图中。点击图像后,它会扩展到全屏,然后再次点击会关闭全屏。在图像已经展开后,我将如何添加缩放以缩放图像。这是我制作全屏图像的代码。
//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()
}
如果你想缩放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;
}
如果您想显示完整图片,请使用 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;
}
我已将点击手势识别器操作添加到我的图像视图中。点击图像后,它会扩展到全屏,然后再次点击会关闭全屏。在图像已经展开后,我将如何添加缩放以缩放图像。这是我制作全屏图像的代码。
//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()
}
如果你想缩放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;
}
如果您想显示完整图片,请使用 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;
}