Iphone 如何使图像在我双击的地方缩放?
我设法让我的应用程序放大我的图像,如果我双击它,但它没有放大我双击的地方!我希望图像以我双击的坐标为中心 我的代码: 在.h文件中:Iphone 如何使图像在我双击的地方缩放?,iphone,ios,uiscrollview,zooming,center,Iphone,Ios,Uiscrollview,Zooming,Center,我设法让我的应用程序放大我的图像,如果我双击它,但它没有放大我双击的地方!我希望图像以我双击的坐标为中心 我的代码: 在.h文件中: - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer; 在.m文件中: UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selecto
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;
在.m文件中:
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];
}
接下来我该怎么办
提前谢谢
/您正在查找的noob位置查看:。它将在指定的视图中为您提供触摸发生的位置。此时,您可以调整视图以使该点成为中心。这里是我以前项目的一个片段。
在
init
-方法中的某个位置添加UITapGestureRecognitizer
:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapRecognized:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[doubleTap release];
双击你视野中的某个地方就会触发此get
- (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)];
}
-(void)DoubleTapRecognited:(UITapGestureRecognitizer*)识别器{
[自zoomToPoint:[识别器位置查看:内容]];
}
-(void)zoomToPoint:(CGPoint)位置{
float zoomScaleBefore=self.zoomScale;
如果(location.x=content.bounds.size.height)location.y=content.bounds.size.height-1;
浮动百分比x=location.x/content.bounds.size.width;
浮动百分比=location.y/content.bounds.size.height;
[自设置ZoomScale:10.0动画:是];
float pox=(percentX*self.contentSize.width)-(self.frame.size.width/2);
浮点poy=(百分比*self.contentSize.height)-(self.frame.size.height/2);
如果(pox)Swift 3.0版本在双击时缩放:
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))
tapRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(tapRecognizer)
谢谢!如果你能写一个我应该使用的代码,我会非常喜欢的!如果不是太多的工作的话!:)这很复杂。你将在指定的视图中获得坐标。如果你指定图像视图,你需要知道图像视图和控制器视图之间的关系。如果你指定控制器视图,你需要知道图像视图在控制器视图中的位置。我不明白该怎么办,所以rry:(非常好用。在这一点上,我觉得重要的是:这段代码来自UIScrollView类。如果有人想使用ScrollView作为出口:“self”。参数需要用ScrollView名称以及“content”(表示UIImageView)扩展。
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))
}
}