2012-02-05 110 views
1

看着這個問題:Prevent UIScrollView from moving contents to top-left,我有確切的問題。UIScrollView在放大時將圖像移動到左上角

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

回到類似的問題,如果我禁用UIScrollViewPanGestureRecognizer,我不能夠再平移縮放圖像。

我在UIScrollView中有一個UIImageView,我想能夠縮放和平移圖像。

如何在放大時禁用移動到左上角的內容?

回答

0

看來我解決我的調整UIScrollView的自動調整大小和Origin在尺寸檢查\屬性檢查器。我沒有選擇Paging Enabled,發生了魔法。

0

當你縮小時,你想讓內容居中嗎?看看這個相似的SO question。真正的答案是向下。

+0

我只是想放大圖像而不能自動放置到某種「隱形網格」。當我放大時,圖像自動移動到左上角 – Phillip 2012-02-05 16:00:23

0

做的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

0

只是爲了防止其他人來到這裏,並沒有其他的答案似乎工作(這是我的情況),我的伎倆是設置scrollView的contentSize。只要將其設置爲任何你正在放大的子視圖的大小,它應該工作。

0

如果我理解你的話,你只想在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