如果我双击它,我设法让我的应用程序放大我的图像,但它不会放大我双击的地方!我希望图像以我双击的坐标为中心!
我的代码:
在.h文件中:
- (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];
}
接下来我该做什么?
提前致谢!
/一个菜鸟
最佳答案
这是我旧项目的一个片段。
在您的 init
方法中的某处添加 UITapGestureRecognizer
:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapRecognized:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[doubleTap release];
双击您 View 中的某处会触发此 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)];
}
关于iphone - 如何让图像在我双击的地方缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759872/