Center an UIImageView on the screen when zoom out

I have an UIImageView inside an UIScrollView. I want that the user can zoom and navigate the image.

This is my work code:

//img is the UIImageView //scroller is the UIScrollView - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return img; } - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"map_screen.png"]; img = [[UIImageView alloc] initWithImage:image]; scroller.delegate = self; scroller.autoresizesSubviews = YES; scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; scroller.contentSize = img.frame.size; scroller.scrollEnabled = YES; scroller.directionalLockEnabled = NO; scroller.userInteractionEnabled = YES; CGSize ivsize = img.frame.size; CGSize ssize = scroller.frame.size; float scalex = ssize.width / ivsize.width; float scaley = ssize.height / ivsize.height; scroller.minimumZoomScale = fmin(1.0, fmax(scaley, scalex)); scroller.zoomScale = fmin(1.0, fmax(scalex, scaley)); [scroller addSubview:img]; img.userInteractionEnabled = YES; }

all works, but this happened: the minimum zoom is the height of the screen. My image has the width bigger than the height, so i want that the minumum zoom is the width.

If i write

scroller.minimumZoomScale = fmin(1.0, scalex);

works, but when the user zooms out, the image is not at the center of the screen, but at the top.

i've tried something like this

CGPoint scrollCenter = [scroller center]; [img setCenter:CGPointMake(scrollCenter.x, scrollCenter.y)];

or

img.center = scroller.center;

but with this solution, the image is not completly scrollable, and if i zoom out, it stay again at the top of the screen, but is not completly visible!

What can i do for fix it?

-------------Problems Reply------------

You have to do it manually while the zooming is in progress using the scrollViewDidZoom delegate function... something like

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

// center the image as it becomes smaller than the size of the screen
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;

}

Category:iphone Views:1 Time:2011-04-29

Related post

  • How do I center a UIImageView within a full-screen UIScrollView? 2009-04-27

    In my application, I would like to present the user with a full-screen photo viewer much like the one used in the Photos app. This is just for a single photo and as such should be quite simple. I just want the user to be able to view this one photo w

  • Using jQuery to center a DIV on the screen 2008-10-17

    How do I go about setting a <div> in the center of the screen using jQuery? --------------Solutions------------- I like adding functions to jQuery so this function would help: jQuery.fn.center = function () { this.css("position","absolute"); th

  • CGDisplayHideCursor, CGDisplayShowCursor and CGWindowListCreateImage problem when screen is zoomed 2011-02-21

    I am developing a Desktop application in which I am reading the screen buffer data and create image out of it. I use the call glReadPixels to read the screen data. If I do not want to show the cursor in the image created from screen buffer, I hide th

  • API to check if the system screen is zoomed or not 2011-03-04

    I am developing an OS X application in which I want to perform some operation if the System screen is zoomed. Is there any API to identify if the screen is zoomed or not. If notification is the solution that will not work for me. Because, the screen

  • How do you center a UIImageView in UITableViewCell regardless of orientation? 2011-06-17

    I'm trying to center a UIImageView width-wise in a UITableViewCell with the following code: // self.cover_image is a UIImage variable // cell is a UITableCellView UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image

  • How to center a Imageview on the screen with a FrameLayout? (with java code) 2011-10-07

    I need to center a imageview on the screen, but programatically, without using XML layouts. I am actually using a FrameLayout, but i dont know how to center it on the FrameLayout. This is the code i have until now: FrameLayout fl = new FrameLayout(th

  • Lumia 520 lock screen is zooming 2012-03-15

    Hi WP team, Sometimes, my Lumia 520 lock screen as well as start screen are zooming out. I couldn't figure it out its happening. Bought it just before 2 weeks. I tried to (pinch) zoom it out manually but of no use. The only solution I found is "reboo

  • When I move IE between screens the zoom changes. 2012-05-12

    Original title: IE Zoom I have screens of different sizes and resolutions. When I move IE between screens the zoom changes. How do I stop this behaviour? Also, I find that the status bar intermittently disappears. Sometimes it just doesn't appear, ot

  • my screen is zoomed in and i dont know how to zoom out. 2012-08-15

    original title: i was carrying my laptop up the stairs and i must have pushed a button. my screen is zoomed in and i dont know how to zoom out. can someone help me? --------------Solutions------------- Simply restart the computer. If this doesn't wor

  • cant get my screen to zoom out help please 2013-05-28

    hi my screen is zoomed right in i cant see whole screen with out movin it about to look at what i need to i cant get it to go normal help please --------------Solutions------------- Have you tried adjusting the Zoom slider on the status bar? If it's

  • Windows Media Center loads to a blank screen 2013-08-10

    media center loads to a blank screen only giving the mouse pointer. i have to use ctr+alt+delete to exit it. tried reinstalling graphics driver and now i am out of solutions --------------Solutions------------- Hi cross20: Let us start with finding o

  • After upgrading to Win 8 I experience similar issues. Screen got zoomed in by an inch at least on my monitor. 2014-01-21

    After upgrading to Win 8 I experience similar issues. Screen got zoomed inby an inch at least on my monitor. I have a new SONY notebook and had downloaded the new drivers, also did a clean instal and have the same issue. Please help! Thanks, Alex ---

  • Onenote automatically scrolls to center my cursor on the screen when I select something or type 2014-04-22

    Whenever I click anywhere on my page, or begin typing, onenote automatically centers my cursor on the screen, and if I scroll up to read something (with my cursor off-center, but still on the screen) onenote immediately brings my cursor back to the c

  • center element in all media screen in specific div 2014-12-18

    i'm working in design using bootstrap framework and i face problem with some screen sizes as this website is responsive i can solve this problem by target every media but i think that there are a solution by using defined class with bootstrap the pro

  • After I upgraded to Windows 8 with Media Center my Media Center Extender has a black screen and no sound. 2015-03-12

    I have a Linksys DMA2100 Media Center Extender that worked fine with Windows 7. After I upgraded to Windows 8 my extender shows that it is connecting, the the screen goes black with no sound. --------------Solutions------------- the third party Links

  • How to center image in a uiimageview/uiscrollview (pre and post zooming) 2011-07-08

    I am trying to center an image (with respect to the iphone screen) such that when the view is loaded the image is center width/height... In my case my view is loaded at pixel points 0,0 of the image itself and if I zoom out the image appears in the t

  • Cocoa Touch - Moving an UIImageView around on screen 2010-07-18

    What's a simple way to let users drag an UIImageView around to position it in the place of their choice. It's for a photography app. Thanks! --------------Solutions------------- First, make a container view that would take care of touch events. Then,

  • Dragging uiimageview into a "trashcan" while zoomed in on a uiscrollview 2010-10-24

    Hey everyone. I've searched the web and really haven't found a problem quite like this. My set up is as follows: I am trying to make an app where a user drags uiimageviewss on a uiview inside a uiscrollview (which can zoom in), but also drag the imag

  • How do I center the page on my screen in MS Word 2010? 2012-11-01

    I recently bought a new computer and also bought a new version of Word (MS Word 2010). I installed Word 2010 on my new computer last week and immediately noticed that the page was distractingly lopsided, with the white page all the way to the left of

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.205 (s). 11 q(s)