2015-12-02 74 views
0

我有一個顯示在我的UIScrollView圖像的小問題,我有這樣的代碼顯示在像照片應用程序分頁的滾動視圖圖像和工程就像一個魅力:照片滾動應用 - 第二UIScrollView中沒有顯示一個UIImageView

- (void)createView 
{ 
    dValue = [dConfiguration objectForKey:@"Buttons"]; 

    int xAxis = 0; 
    int iTag = 0; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenRect.size.width, screenRect.size.height)]; 
    NSString *imageName = @""; 
    for (NSString* sProperty in dValue) 
    { 
     imageName = [util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]; 
     CGRect imageFrame = CGRectMake(xAxis + ((screenRect.size.width - 320)/2), 0, 320, screenRect.size.height); 
     UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:imageFrame]; 

     [self LoadImage:tempImageView imageName:imageName]; 

     tempImageView.userInteractionEnabled = YES; 
     tempImageView.contentMode = UIViewContentModeScaleAspectFit; 

     xAxis += self.view.frame.size.width; 
     [uiView addSubview:tempImageView]; 

     iTag++; 
    } 
    self.pcPage.hidden = NO; 
    [self.pcPage setNumberOfPages:iTag]; 
    self.pcPage.pageIndicatorTintColor = PAGECONTROL_TINTCOLOR; 
    self.pcPage.currentPageIndicatorTintColor = PAGECONTROL_CURRENTPAGE; 
    [_svScroll setContentSize:CGSizeMake(xAxis, 0)]; 
    [_svScroll addSubview:uiView]; 
} 

但現在我想添加縮放功能,所以我已經改變了我的代碼,並且我添加了一個額外的UIScrollView。

像他們在這裏說:How to zoom in/out an UIImage object when user pinches screen?

- (void)createView 
{ 
    dValue = [dConfiguration objectForKey:@"Buttons"]; 

    int xAxis = 0; 
    int iTag = 0; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenRect.size.width, screenRect.size.height)]; 
    NSString *imageName = @""; 
    for (NSString* sProperty in dValue) 
    { 
     imageName = [util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]; 
     CGRect imageFrame = CGRectMake(xAxis + ((screenRect.size.width - 320)/2), 0, 320, screenRect.size.height); 
     UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:imageFrame]; 

     [self LoadImage:tempImageView imageName:imageName]; 

     tempImageView.userInteractionEnabled = YES; 
     tempImageView.contentMode = UIViewContentModeScaleAspectFit; 

     xAxis += self.view.frame.size.width; 

     UIScrollView *uiScrollView = [[UIScrollView alloc] initWithFrame:imageFrame]; 
     uiScrollView.minimumZoomScale = 1.0; 
     uiScrollView.maximumZoomScale = 2.0; 
     uiScrollView.delegate = self; 
     [uiScrollView setContentSize:CGSizeMake(320, screenRect.size.height)]; 

     [uiScrollView addSubview:tempImageView]; 
     [uiView addSubview:uiScrollView]; 

     iTag++; 
    } 
    self.pcPage.hidden = NO; 
    [self.pcPage setNumberOfPages:iTag]; 
    self.pcPage.pageIndicatorTintColor = PAGECONTROL_TINTCOLOR; 
    self.pcPage.currentPageIndicatorTintColor = PAGECONTROL_CURRENTPAGE; 
    [_svScroll setContentSize:CGSizeMake(xAxis, 0)]; 
    [_svScroll addSubview:uiView]; 
} 

此代碼只顯示第一個圖像,我不知道我做錯了。

我艱難的在第一,也許新的UIScrollView是不是在正確的位置,所以我的的UIScrollView的改變底色的顏色(以隨機的方式),我可以看到2滾動主滾動以及分頁:

iColor = 50; 
[uiScrollView setBackgroundColor:[UIColor colorWithRed:iColor/255.0 green:131.0/255.0 blue:19.0/255.0 alpha:1]]; 
iColor += 50; 

然後我強硬的觀點的UIImageView是不是取消了UIView,我測試了它:這裏

UIImageView *v1 = [[[[uiView subviews] objectAtIndex:0] subviews] objectAtIndex:0]; 
UIImageView *v2 = [[[[uiView subviews] objectAtIndex:1] subviews] objectAtIndex:0]; 

UIScrollView *v3 = [[uiView subviews] objectAtIndex:0]; 
UIScrollView *v4 = [[uiView subviews] objectAtIndex:1]; 

UIImageView *v5 = [[[[[[_svScroll subviews] objectAtIndex:0] subviews] objectAtIndex:0] subviews] objectAtIndex:0]; 
UIImageView *v6 = [[[[[[_svScroll subviews] objectAtIndex:0] subviews] objectAtIndex:1] subviews] objectAtIndex:0]; 

沒有什麼是零,我甚至可以看到快速瀏覽功能的圖像。

如果我打印值這個結果如下:V1的

印刷描述:

的UIImageView:0x156d6b90; frame =(0 0; 320 480); opaque = NO;層= 的CALayer:V2的0x156d4810

印刷描述:

的UIImageView:0x156dc0f0;幀=(320 0; 320 480); opaque = NO;層 =的CALayer:V3的0x156bdc90

印刷描述:

的UIScrollView:0x1639a000; frame =(0 0; 320 480); clipsToBounds = YES; gestureRecognizers = NSArray:0x1567a060; layer = CALayer:0x156d5c90; contentOffset:{0,0}; contentSize:{320,480}的V4

印刷描述:

的UIScrollView:0x15b7d800;幀=(320 0; 320 480); clipsToBounds = YES; gestureRecognizers = NSArray:0x155dae10; layer = CALayer: 0x155de3d0; contentOffset:{0,0}; contentSize:{320,480}的V5

印刷描述:

的UIImageView:0x156d6b90; frame =(0 0; 320 480); opaque = NO;層= 的CALayer:V6的0x156d4810

印刷描述:

的UIImageView:0x156dc0f0;幀=(320 0; 320 480); opaque = NO;層 =的CALayer:0x156bdc90

正如你可以看到它看起來像框架和內容的大小是好的,但由於某些原因,我只能看到第一張圖像。

我也測試過改變圖像順序,看它是否是圖像中的東西,但結果是一樣的。

我也加入了這個代碼來控制變焦:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    return [[scrollView subviews] objectAtIndex:0]; 
} 

而這個計算頁:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGFloat pageWidth = self.svScroll.frame.size.width; 
    int page = floor((self.svScroll.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    self.pcPage.currentPage = page; 
} 

所以在最後,第一個圖像完美的作品,我可以放大和縮小,我可以滾動到下一頁,即使放大了圖像,也可以滾動到下一頁,但由於某種原因,第二張圖像無法工作(或除第一張外)。

這裏有一些屏幕截圖:

enter image description here

enter image description here

enter image description here

回答

0

的問題是,我用的是相同的幀的UIScrollView的,爲的UIImageView:

對於UIScrollView例如你hav E本座標:

(0,0) (320,0) (640,0) ...

但因爲所有的UIImageView是UIScrollView的內部,你必須設置的座標( 0,0)總是。

這是代碼現在的樣子:

- (void)createView 
{ 
    dValue = [dConfiguration objectForKey:@"Buttons"]; 

    int xAxis = 0; 
    int iTag = 0; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenRect.size.width, screenRect.size.height)]; 
    NSString *imageName = @""; 
    for (NSString* sProperty in dValue) 
    { 
     imageName = [util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]; 
     UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, screenRect.size.height)]; 
     [self LoadImage:tempImageView imageName:imageName]; 
     tempImageView.userInteractionEnabled = YES; 
     tempImageView.contentMode = UIViewContentModeScaleAspectFit; 

     UIScrollView *uiScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xAxis + ((screenRect.size.width - 320)/2), 0, 320, screenRect.size.height)]; 
     uiScrollView.minimumZoomScale = 1.0; 
     uiScrollView.maximumZoomScale = 2.5; 
     uiScrollView.delegate = self; 

     [uiScrollView addSubview:tempImageView]; 
     [uiView addSubview:uiScrollView]; 

     xAxis += self.view.frame.size.width; 

     iTag++; 
    } 
    self.pcPage.hidden = NO; 
    [self.pcPage setNumberOfPages:iTag]; 
    self.pcPage.pageIndicatorTintColor = PAGECONTROL_TINTCOLOR; 
    self.pcPage.currentPageIndicatorTintColor = PAGECONTROL_CURRENTPAGE; 

    CGRect newFrame = uiView.frame; 

    newFrame.size.width = xAxis; 
    [uiView setFrame:newFrame]; 

    [_svScroll setContentSize:CGSizeMake(xAxis, 0)]; 
    [_svScroll addSubview:uiView]; 
} 

感謝所有。

相關問題