objective c - How do I center a UIImageView within a full-screen UIScrollView? -
In my application, I would like to present the user with a full-screen photo viewer such as the Photos app It's just for a picture and as it should be very simple. I just want to be able to see this one picture with the ability of the user to zoom and pan.
I have most of the work in it and, if I do not focus on my UIImageView, then everything behaves properly. However, I really want to focus UIImageView on the screen when the image is zoomed out adequately. I do not want it stuck in the top-left corner of the scroll view.
Once I try to focus this view, my vertical scroll area appears to be larger than that. For example, once I zoom a bit, I can scroll around 100 pixels from the image. What am i doing
@interface MyPhotoViewController: UIViewController & lt; UIScrollViewDelegate & gt; {UIImage * photo; UIImageView * imageView; } - (id) initWithPhoto: (UIImage *) aphoto; @end @implementation MyPhotoViewController - (ID) initWithPhoto: (UIImage *) aPhoto {if (self = [super init]) {picture = [maintaining a photo]; // Some 3.0 SDK code here to ensure that this view is a full-screen // layout. } Healthy return; } - (Zero) Dailyock [[Photo release]; [See image release]; [Super DeLoc]; } - (zero) loadView {// This is a UIScrollView to set the main view of the UIViewController UIScrollView * scrollView = [[UIScrollView alloc] init]; [Self set view: scrollview]; [Scroll view release]; } - (zero) viewDidoadload [[Super Viewedload]; // Start Scroll view CGSize photoSize = [Photo size]; UIScrollView * scrollView = (UIScrollView *) [Self View]; [Delegate set ScrollView: self]; [ScrollView setBackgroundColor: [UIColor blackColor]]; // Create Image View. We push the original (0, -44) to make sure that the view displays behind the navigation bar. ImageView = [[UIImageView alloc] initWithFrame: CGRectMake (0.0, -44.0, photoSize.width, photoSize.height)]; [ImageView setImage: photo]; [Add ScrollView Subview: imageView]; // Configure zooming CGSize screen size = [[UScreen main screen] boundary]. Shape; CGFloat widthRatio = Screen size Width / photoSize.width; CGflot height RTS = ScreenSyz Highlight / Photos height; CGFloat initialZoom = (widthRatio & gt; Height Ratio)? Height row width [ScrollView SetMixiumZScackel: 3.0]; [ScrollView SetiniumZoomScale: Initialization]; [ScrollView SetzumScale: Initial Zoom]; [Scrollview setbunsuns: yes]; [ScrollView setContentSize: CGSizeMake (photoSize.width * initialZoom, photoSize.height * initial zoom)]; // center photo again we increase the center to 44 pixels for the transparent navigation bar. CGPoint scrollCenter = [scrollView center]; [ImageView Set Center: CGPointMake (scrollCenter.x, scrollCenter.y - 44.0)]; } - (Zero) ViewVillagePower: (BOOL) Animated {[Super Viewer: Animated]; [[[Navigation Controller] Navbarbar] Set Bars Style: UIBarStyleBlackTranslucent]; [[UIApplication ShareApp] setStatusBarStyle: Animated UIStatusBarStyleBlackTranslucent: Yes]; } - (zero) view specially: (BOOL) animated ([super visual shape: animated]; [[[self navigation controller] navigation bar] set bars style: UIBarStyleDefault]; [[UIApplication shared application] setStatusBarStyle: UIStatusBarStyleDefault animated: Yes];} - (UIView *) viewForZoomingInScrollView: (UIScrollView *) scrollView {return imageView;} @end
Add it to your subclass of UIScrollView and < Code> tileContainerView
based on
code> with your image or tiled view:
- ( Zero) Layouts SubViews {[Super Layout Edit]; // The image becomes smaller than the size of the screen in the form of CGSize limitations size = self.bounds.size; CGRTF frameToocenter = tileSystemWare Frame; // frame horizontally; Frame.center.ize.width & lt; boundizeSize.width) frameToCenter.origin.x = (boundsis. Width - FrameTuinter. Size.width) / 2; Else framescenter.organ.x = 0; // center vertical if (framescenter. Size.height & lt; boundizeSize.height) frameToCenter.origin.y = (borders.height - frameToCenter.size.height) / 2; Other Frame Tonicers Orgin 0 = 0; Tile Container View Frame = framescenter; }
Comments
Post a Comment