2013-08-23 63 views
3

我正在做一個項目,其中我需要在單個滾動視圖中使用縮放選項的兩個圖像瀏覽。 UIScrollView允許一個視圖輕鬆進行縮放。但我的問題不是那樣的。我在滾動視圖中並排顯示兩個圖像。當我嘗試縮放圖像時,它不起作用是否有任何選項可以同時縮放兩個圖像視圖。請給我一些建議。捏在單個滾動視圖上的兩個ImageViews縮放在IOS上

在此先感謝

+0

所以你的問題是,當你放大,你的兩個圖像應該是變焦...我對嗎??? – NiravPatel

+0

是的你是正確 – btmanikandan

+1

我認爲你應該添加兩個imageview在一個視圖和viewForZooming方法,你應該返回多數民衆贊成 –

回答

0

在視圖中添加兩個ImageView的並替換此方法

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return view; 
} 
2

下面是在單一滾動視圖縮放兩個圖像代碼

我已經在一個UIView添加了兩個UIImageView的,然後我已經添加了UIViewUIScrollView。以下是我的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.vwImages = [[UIView alloc]initWithFrame:CGRectMake(0, 0,100,200)]; 

    // 1 
    UIImage *image = [UIImage imageNamed:@"imgBaskteBall2.jpeg"]; 
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image]; 
    imageView1.frame = CGRectMake(0,0,100,100); 
    [self.vwImages addSubview:imageView1]; 

    UIImage *image2 = [UIImage imageNamed:@"imgBaskteBall1.jpeg"]; 
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2]; 
    imageView2.frame = CGRectMake(0, 100, 100, 100); 
    [self.vwImages addSubview:imageView2]; 

    // 2 

    [self.scrollView addSubview:self.vwImages]; 

    self.scrollView.contentSize = self.vwImages.frame.size; 

    // 3 
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)]; 
    doubleTapRecognizer.numberOfTapsRequired = 2; 
    doubleTapRecognizer.numberOfTouchesRequired = 1; 
    [self.scrollView addGestureRecognizer:doubleTapRecognizer]; 

    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)]; 
    twoFingerTapRecognizer.numberOfTapsRequired = 1; 
    twoFingerTapRecognizer.numberOfTouchesRequired = 2; 
    [self.scrollView addGestureRecognizer:twoFingerTapRecognizer]; 
} 


- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // 4 
    CGRect scrollViewFrame = self.scrollView.frame; 
    CGFloat scaleWidth = scrollViewFrame.size.width/self.scrollView.contentSize.width; 
    CGFloat scaleHeight = scrollViewFrame.size.height/self.scrollView.contentSize.height; 
    CGFloat minScale = MIN(scaleWidth, scaleHeight); 
    self.scrollView.minimumZoomScale = minScale; 

    // 5 
    self.scrollView.maximumZoomScale = 10.0f; 
    self.scrollView.zoomScale = minScale; 

    // 6 
    [self centerScrollViewContents]; 
} 


- (void)centerScrollViewContents { 
    CGSize boundsSize = self.scrollView.bounds.size; 
    CGRect contentsFrame = self.vwImages.frame; 

    if (contentsFrame.size.width < boundsSize.width) { 
     contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
    } else { 
     contentsFrame.origin.x = 0.0f; 
    } 

    if (contentsFrame.size.height < boundsSize.height) { 
     contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
    } else { 
     contentsFrame.origin.y = 0.0f; 
    } 

    self.vwImages.frame = contentsFrame; 
} 

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer { 
    // 1 
    CGPoint pointInView = [recognizer locationInView:self.imageView]; 

    // 2 
    CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f; 
    newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale); 

    // 3 
    CGSize scrollViewSize = self.scrollView.bounds.size; 

    CGFloat w = scrollViewSize.width/newZoomScale; 
    CGFloat h = scrollViewSize.height/newZoomScale; 
    CGFloat x = pointInView.x - (w/2.0f); 
    CGFloat y = pointInView.y - (h/2.0f); 

    CGRect rectToZoomTo = CGRectMake(x, y, w, h); 

    // 4 
    [self.scrollView zoomToRect:rectToZoomTo animated:YES]; 
} 

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer { 
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view 
    CGFloat newZoomScale = self.scrollView.zoomScale/1.5f; 
    newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale); 
    [self.scrollView setZoomScale:newZoomScale animated:YES]; 
} 

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    // Return the view that you want to zoom 
    return self.vwImages; 
} 

- (void)scrollViewDidZoom:(UIScrollView *)scrollView { 
    // The scroll view has zoomed, so you need to re-center the contents 
    [self centerScrollViewContents]; 
} 
+0

'CGPoint pointInView = [識別器locationInView:self.imageView];'如何獲得Imageview? – user2526811

2

的UIView使對象爲容器例如viewContainer

[viewContainer addSubView:imageView1]; 

[viewContainer addSubView:imageView2]; 

[scrollView addSubView:viewContainer]; 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return viewContainer; 
} 

添加

相關問題