2010-11-01 101 views
0

在我的應用程序中,我在UIScrollView中使用了UIImageView,並使用了代碼。UIScrollView放大問題?

- (void)viewDidLoad 

{ 

scrollV.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height); 

    [super viewDidLoad]; 

} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 

{ 

    return imageView; 
} 

代碼工作正常,但是當我放置的UIButton上滾動視圖的ImageView的和我縮放scrollV之後,ImageView的變焦完美根據滾動視圖但UIButton的和不被放大。

我想要當我放大imageview,我放在imageView上的按鈕也放大相對於縮放imageView。

+0

@Jacob Relkin,你有什麼想法嗎? – Tirth 2010-11-01 06:25:00

回答

0

我成功找出寫在了上面question.I的解決方案,

- (void)viewDidLoad 

{ 

    bgViewOfImage.contentStretch = CGRectMake(ImageView.frame.origin.x, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height); 
    scrollV.contentSize = CGSizeMake(bgViewOfImage.frame.size.width, bgViewOfImage.frame.size.height); 
    [super viewDidLoad]; 
} 


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 

{ 

    return bgViewOfImage; 
} 

我創建一個層級中插入的主視圖的UIView的,則返回該視圖我拖動其他控件像UIImageview和UIButtons它現在完美的工作和縮放每個控件。

1

創建一個代理UIView實例作爲scrollview的子視圖,並將所有要放大的UI元素放入其中。然後從viewForZoomingInScrollView方法

0
CGSize imageView = [photo size]; 
    UIScrollView *scrollView = (UIScrollView *)[self view]; 
    [scrollView setDelegate:self]; 
    [scrollView setContentSize:photoSize]; 
    [scrollView setBackgroundColor:[UIColor blackColor]]; 
    [scrollView setShowsHorizontalScrollIndicator:NO]; 
    [scrollView setShowsVerticalScrollIndicator:NO]; 

    // Create the image view. 
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)]; 
    [imageView setImage:photo]; 
    [scrollView addSubview:imageView]; 

    // Configure zooming. 
    CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
    CGFloat widthRatio = screenSize.width/photoSize.width; 
    CGFloat heightRatio = screenSize.height/photoSize.height; 
    CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio; 
    [scrollView setMaximumZoomScale:3.0]; 
    [scrollView setMinimumZoomScale:initialZoom]; 
    [scrollView setZoomScale:initialZoom]; 
    [scrollView setBouncesZoom:YES]; 

    // Center the photo. 
    CGFloat topInset = (screenSize.height - photoSize.height * initialZoom)/2.0; 
    if (topInset > 0.0) 
    { 
     [scrollView setContentInset:UIEdgeInsetsMake(topInset - 44.0, 0.0, 0.0, 0.0)]; 
    } 
} 

#pragma mark - 
#pragma mark UIScrollViewDelegate 

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

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
    CGSize photoSize = [photo size]; 
    CGFloat topInset = (screenSize.height - photoSize.height * [scrollView zoomScale])/2.0; 
    if (topInset < 0.0) 
    { 
     topInset = 0.0; 
    } 

    [scrollView setContentInset:UIEdgeInsetsMake(topInset, 0.0, -topInset, 0.0)]; 
} 
+0

您應該重新安排您的人類閱讀代碼。 – AechoLiu 2010-11-01 09:34:16