UIScrollView在放大时将图像移动到左上角

问题描述:

看着这个问题:Prevent UIScrollView from moving contents to top-left,我有确切的问题。UIScrollView在放大时将图像移动到左上角

我使用本教程:http://cocoadevblog.heroku.com/iphone-tutorial-uiimage-with-zooming-tapping-rotation

回到类似的问题,如果我禁用UIScrollViewPanGestureRecognizer,我不能够再平移缩放图像。

我在UIScrollView中有一个UIImageView,我想能够缩放和平移图像。

如何在放大时禁用移动到左上角的内容?

看来我解决我的调整UIScrollView的自动调整大小和Origin在尺寸检查\属性检查器。我没有选择Paging Enabled,发生了魔法。

当你缩小时,你想让内容居中吗?看看这个相似的SO question。真正的答案是向下。

+0

我只是想放大图像而不能自动放置到某种“隐形网格”。当我放大时,图像自动移动到左上角 – Phillip 2012-02-05 16:00:23

做的UIScrollView的子类,而这种方法添加到它:

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    // center the image as it becomes smaller than the size of the screen 
    CGSize boundsSize = self.bounds.size; 

    //get the subView that is being zoomed 
    UIView *subView = [self.delegate viewForZoomingInScrollView:self]; 

    if(subView) 
    { 
    CGRect frameToCenter = subView.frame; 

    // center horizontally 
    if (frameToCenter.size.width < boundsSize.width) 
     frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2; 
    else 
     frameToCenter.origin.x = 0; 

    // center vertically 
    if (frameToCenter.size.height < boundsSize.height) 
     frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2; 
    else 
     frameToCenter.origin.y = 0; 

    subView.frame = frameToCenter; 
    } 

    else 
     NSLog(@"No subView set for zooming in delegate"); 
} 
+1

仍然无法修复它.. – Phillip 2012-02-05 16:38:30

+0

您是否注意到有任何更改?如果你不这样做,那么你做错了,因为这应该defo工作。 – 2012-02-05 16:58:53

+0

让我们一步一步来做。 subView是另一个UIScrollView的权利? layoutsubviews我想知道它是什么。 – Phillip 2012-02-05 17:04:44

只是为了防止其他人来到这里,并没有其他的答案似乎工作(这是我的情况),我的伎俩是设置scrollView的contentSize。只要将其设置为任何你正在放大的子视图的大小,它应该工作。

如果我理解你的话,你只想在ImageView放大时允许滚动,然后输入scrollView.zoomScale > 1。对于我的应用程序要求,我正在使用此。

如下添加UIScrollView的委托方法并检查。

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView 
{ 
    CGFloat offsetY = 0; 
    if (aScrollView.zoomScale > 1) 
     offsetY = aScrollView.contentOffset.y; 

    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, offsetY)]; 
} 
+0

你好Tanya,你正在回答一个已经被正确回答的非常古老的问题。你能否在你的帖子中解释为什么你的答案更好,或者在某些情况下比其他答案更可取? – 2017-05-29 14:38:22