2009-04-27 7 views
19

내 응용 프로그램에서 사진 응용 프로그램에 사용 된 것과 비슷한 전체 화면 사진 뷰어를 사용자에게 제공하고 싶습니다. 이것은 단지 하나의 사진을위한 것이므로 매우 간단해야합니다. 사용자가이 사진을 확대/축소 및 이동하는 기능으로 볼 수 있기를 바랍니다.전체 화면 UIScrollView 내에서 UIImageView를 가운데에 배치하는 방법은 무엇입니까?

나는 대부분 작동하고 있습니다. 그리고 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

(문서에서)

.

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 내부의 서브 뷰의 실제 크기에 관계없이 zoomscale이 전체 contentSize에 적용되기 때문입니다 (귀하의 경우에는 imageView 임). contentSize 높이는 항상 scrollView 프레임의 높이보다 크거나 같지만 결코 작지는 않습니다. 따라서 zoom을 적용 할 때 contentSize의 높이에 zoomScale 요소가 곱 해져서 수직 스크롤의 100-pixel 픽셀을 얻게됩니다.

2

UIViewAutoresizing 옵션을 확인 했습니까? 이 코드는 아이폰 OS의 대부분의 버전에서 작동합니다 (위쪽 3.1에서 작동하도록 테스트되었습니다)

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; 
관련 문제