我设法让我的应用程序放大我的图像,如果我双击它,但它不放大我双击的地方!我希望图像以我双击的坐标为中心!
我的代码:
<。>文件中的:
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;
<。>文件中的:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.scroll addGestureRecognizer:doubleTap];
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if(self.scroll.zoomScale > scroll.minimumZoomScale)
[self.scroll setZoomScale:scroll.minimumZoomScale animated:YES];
else
[self.scroll setZoomScale:1.6 animated:YES];
}
接下来我该怎么做?
提前致谢!
/ A菜鸟
答案 0 :(得分:8)
这是我旧项目的一个片段
在UITapGestureRecognizer
- 方法的某处添加init
:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapRecognized:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[doubleTap release];
在你视图的某个地方双击一下这个获取。
- (void)doubleTapRecognized:(UITapGestureRecognizer*)recognizer {
[self zoomToPoint:[recognizer locationInView:content]];
}
- (void)zoomToPoint:(CGPoint)location {
float zoomScaleBefore = self.zoomScale;
if (location.x<=0) location.x = 1;
if (location.y<=0) location.y = 1;
if (location.x>=content.bounds.size.width) location.x = content.bounds.size.width-1;
if (location.y>=content.bounds.size.height) location.y = content.bounds.size.height-1;
float percentX = location.x/content.bounds.size.width;
float percentY = location.y/content.bounds.size.height;
[self setZoomScale:10.0 animated:YES];
float pox = (percentX*self.contentSize.width)-(self.frame.size.width/2);
float poy = (percentY*self.contentSize.height)-(self.frame.size.height/2);
if (pox<=0) pox = 1;
if (poy<=0) poy = 1;
[self scrollRectToVisible:
CGRectMake(pox, poy, self.frame.size.width, self.frame.size.height)
animated:(self.zoomScale == zoomScaleBefore)];
}
答案 1 :(得分:5)
Swift 3.0 版本可以双击放大:
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))
tapRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(tapRecognizer)
...
func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)
if scale != scrollView.zoomScale {
let point = gestureRecognizer.location(in: imageView)
let scrollSize = scrollView.frame.size
let size = CGSize(width: scrollSize.width / scale,
height: scrollSize.height / scale)
let origin = CGPoint(x: point.x - size.width / 2,
y: point.y - size.height / 2)
scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
print(CGRect(origin: origin, size: size))
}
}
答案 2 :(得分:4)
您正在寻找-locationInView:
。它会在指定视图中为您提供触摸发生的位置。此时,您可以调整视图以使该点成为中心。