2011-06-22 17 views
2

UIScrollViewの中にUIImageViewがあるので、ズームが可能ですが、ズームは機能しますが、UIImageViewを中央に保持せずに終わり、黒いスペースを残します上に。UIScrollViewのUIImageViewが中心にズームしない

これは私が持っているものです。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    scroller.bouncesZoom = YES; 
    scroller.clipsToBounds = YES; 

    float scale = image.size.width/320; 
    UIImage *scaled = [UIImage imageWithCGImage:[image CGImage] scale:scale orientation:UIImageOrientationUp]; 

    imageView = [[UIImageView alloc] initWithImage:scaled]; 
    imageView.userInteractionEnabled = YES; 
    [imageView setCenter:CGPointMake(160,240)]; 

    [scroller addSubview:imageView]; 
    scroller.contentSize = [imageView frame].size; 

    scroller.maximumZoomScale = 4.0; 
    scroller.minimumZoomScale = 1.0; 
    scroller.zoomScale  = 1.0; 
} 

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

任意の提案ですか?以下は

+0

去年のWWDCでスクロールビューのデザインに関する話がありました。それを見て、それは本当に私の多くを助けました:) – Julian

答えて

-1

私は私のアプリで使用したコードは、たぶん、あなたはそれから何かを取ることができます(WWDCサンプルから適応)です:)

CGSize imageSize =[image size]; 
    CGSize boundsSize = scrollview.bounds.size; 
    CGRect frameToCenter = imageview.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; 

    imageview.frame = frameToCenter; 

    CGFloat xScale = boundsSize.width/imageSize.width; // the scale needed to perfectly fit the image width-wise 
    CGFloat yScale = boundsSize.height/imageSize.height; // the scale needed to perfectly fit the image height-wise 
    CGFloat minScale = MIN(xScale, yScale);     // use minimum of these to allow the image to become fully visible 

    // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the 
    // maximum zoom scale to 0.5. 
    CGFloat maxScale = 1.0/[[UIScreen mainScreen] scale]; 

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
    if (minScale > maxScale) { 
     minScale = maxScale; 
    } 

    scrollview.contentSize=imageSize; 
    scrollview.maximumZoomScale=maxScale; 
    scrollview.minimumZoomScale=minScale; 
    scrollview.zoomScale=1; 
+1

どこでこのコードを使用しますか? – thechengster

0

私はthisが何をしたいと思います。

+0

これは私が欲しいものですが、オーバーライドすることなく行う方法はないと思います – thechengster

0

ページ付けが必要とされない場合は、無効にページネーションそれが正常に動作し

scrollview.pagingEnabled = NO;

関連する問題