2009-04-27 11 views
19

私のアプリケーションでは、Photosアプリケーションで使用されているようなフルスクリーンの写真ビューアをユーザーに提示したいと思います。これは単一の写真のためのものであり、非常に単純なものでなければならない。私はちょうどズームとパンの機能を持つこの1つの写真を見ることができるようにしたい。UIImageViewを全画面UIScrollViewの中央に配置するにはどうすればよいですか?

私はそのほとんどが働いています。私のUIImageViewを中央に配置しないと、すべてが完全に動作します。しかし、私は実際には、画像が十分にズームアウトされたときにUIImageViewを画面の中央に配置したいと思っています。私はそれがスクロールビューの左上隅に止まって欲しくない。

このビューを中央に配置しようとすると、垂直スクロール可能領域が大きくなるように見えます。そのため、少しズームインすると、イメージの上端から約100ピクセルスクロールすることができます。私は間違って何をしていますか?

@interface MyPhotoViewController : UIViewController <UIScrollViewDelegate> 
{ 
    UIImage* photo; 
    UIImageView *imageView; 
} 
- (id)initWithPhoto:(UIImage *)aPhoto; 
@end 

@implementation MyPhotoViewController 

- (id)initWithPhoto:(UIImage *)aPhoto 
{ 
    if (self = [super init]) 
    { 
     photo = [aPhoto retain]; 

     // Some 3.0 SDK code here to ensure this view has a full-screen 
     // layout. 
    } 

    return self; 
} 

- (void)dealloc 
{ 
    [photo release]; 
    [imageView release]; 
    [super dealloc]; 
} 

- (void)loadView 
{ 
    // Set the main view of this UIViewController to be a UIScrollView. 
    UIScrollView *scrollView = [[UIScrollView alloc] init]; 
    [self setView:scrollView]; 
    [scrollView release]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Initialize the scroll view. 
    CGSize photoSize = [photo size]; 
    UIScrollView *scrollView = (UIScrollView *)[self view]; 
    [scrollView setDelegate:self]; 
    [scrollView setBackgroundColor:[UIColor blackColor]]; 

    // Create the image view. We push the origin to (0, -44) to ensure 
    // that this view displays behind the navigation bar. 
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, -44.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]; 
    [scrollView setContentSize:CGSizeMake(photoSize.width * initialZoom, 
     photoSize.height * initialZoom)]; 

    // Center the photo. Again we push the center point up by 44 pixels 
    // to account for the translucent navigation bar. 
    CGPoint scrollCenter = [scrollView center]; 
    [imageView setCenter:CGPointMake(scrollCenter.x, 
     scrollCenter.y - 44.0)]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent]; 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [[[self navigationController] navigationBar] setBarStyle:UIBarStyleDefault]; 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; 
} 

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

@end 
+0

そのsetZoomScaleコールは何ですか? ZoomScrollViewを使用していますか? –

答えて

26

このコードは、iOSのほとんどのバージョンで動作するはずです(と3.1上向きで動作するようにテストされています)。

これはApple WWDC code for PhotoScrollerに基づいています。

UIScrollViewのサブクラスに以下を追加し、あなたの画像やタイルを含むビューでtileContainerViewを置き換える:

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    // center the image as it becomes smaller than the size of the screen 
    CGSize boundsSize = self.bounds.size; 
    CGRect frameToCenter = tileContainerView.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; 

    tileContainerView.frame = frameToCenter; 
} 
+1

美しい、美味しい、お仕事。 – kim3er

+0

これは完全に動作し、私はそれを常に使用します。 – bentford

+0

thxが完璧に動作する:D –

-1

おそらく、画像表示のスクロールビュー=境界の境界を設定し、それを含むビューにスクロールビューを中央にしたいです。スクロールビュー内のビューを上からオフセットして配置すると、その上に空のスペースができます。

+0

ありがとうございますが、それは違いがないようです。 [scrollView setBounds:[imageView bounds]];を呼び出します。イメージが画面の全垂直スペースを占めていなくても、上下にスクロールすることができます。 –

1

IBを使用してスクロールビューを追加していますか?スクロールビューの自動サイズ調整オプションを添付画像に変更します。 alt text http://img527.imageshack.us/img527/1607/picture1mqc.th.png

+0

私はIBを使ってUIScrollViewを追加していません。しかし、私はスクロールビューのサイズが正しいと思います。スクロールビューの背景色を紫色に変更すると、画面全体が常に画面全体を占めることがわかります。 –

0

この問題を解決できましたか?私は同様の問題で立ち往生しています。私はそれの背後にある理由は、scrollView内のサブビューの実際のサイズ(あなたの場合はimageView)にかかわらず、全体のcontentSizeにzoomScaleが適用されるためだと思います。 contentSizeの高さは、scrollViewフレームの高さと常に等しいか、それよりも大きいようですが、決して小さくはありません。したがって、ズームを適用すると、contentSizeの高さにzoomScaleの係数も乗算されます。そのため、垂直スクロールの余分な100ピクセルのピクセルが得られます。

2

あなたはUIViewAutoresizingオプションをチェックしましたか? (ドキュメントから)

UIViewAutoresizing 
Specifies how a view is automatically resized. 

enum { 
    UIViewAutoresizingNone     = 0, 
    UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 
    UIViewAutoresizingFlexibleWidth  = 1 << 1, 
    UIViewAutoresizingFlexibleRightMargin = 1 << 2, 
    UIViewAutoresizingFlexibleTopMargin = 1 << 3, 
    UIViewAutoresizingFlexibleHeight  = 1 << 4, 
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5 
}; 
typedef NSUInteger UIViewAutoresizing; 
関連する問題