20

私のアプリケーションでは、写真アプリで使用されているものとよく似たフルスクリーンのフォト ビューアーをユーザーに表示したいと考えています。これは 1 枚の写真のためのものであり、非常に単純なはずです。ユーザーがこの 1 枚の写真をズームとパンで表示できるようにしたいだけです。

私はそれのほとんどを機能させています。そして、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
4

5 に答える 5

28

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

これは、 PhotoScroller の Apple WWDC コードに基づいています。

以下を のサブクラスに追加し、画像またはタイルを含むビュー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;
}
于 2010-08-13T17:00:00.993 に答える
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;
于 2010-01-29T21:24:55.003 に答える
1

スクロールビューを追加するためにIBを使用していますか?スクロールビューの自動サイズ変更オプションを添付画像に変更します。 代替テキスト

于 2009-04-28T04:35:29.757 に答える
0

その背後にある理由は、scrollView内のサブビュー(この場合はimageView)の実際のサイズに関係なく、zoomScaleがcontentSize全体に適用されるためだと思います。contentSizeの高さは、scrollViewフレームの高さと常に同じかそれ以上のように見えますが、それより小さくなることはありません。したがって、ズームを適用すると、contentSizeの高さにzoomScale係数も掛けられます。そのため、100ピクセル程度の垂直スクロールが追加されます。

于 2009-08-04T19:22:32.593 に答える
-1

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

于 2009-04-27T21:29:43.993 に答える