iphone - 如何让图像在我双击的地方缩放?

标签 iphone ios uiscrollview zooming center

如果我双击它,我设法让我的应用程序放大我的图像,但它不会放大我双​​击的地方!我希望图像以我双击的坐标为中心!

我的代码:

在.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/

相关文章:

ios - 从今天小部件打开密码屏幕

iphone - 自动滚动scrollview

UINavigationController 更改后 ios UIScrollView 图像缩放发生变化

iphone - 简单的核心数据获取非常慢

iphone - 在后台运行时接收可达性通知?

iphone - 如何清除UIWebView缓存?

ios - 网页 View :How to detect if I am on the original web page and execute a certain method

objective-c - 当我倒置 iPad 时旋转应用程序中的按钮

ios - 带 POST 参数的 AFNetworking 上传任务

ios - uiscrollview 中的 UIViewControllers 调用 viewdidappear