centred image scalled in scrollview

Discussion in 'iOS Programming' started by ghanalupo, Sep 17, 2009.

  1. ghanalupo macrumors newbie

    Sep 14, 2009
    I have managed to get an image to scale correctly into a scrollview so that no matter how wide the image is it initially displays scaled down so the width is 320 and the image fits nicely in the view.

    The problem is the height is not centred so the image although sitting nicely scaled width wise gets pushed up at origin 0,0 of the scrollview.

    I have been trying to figure out how to get the image to initially display scaled so you can see the entire image scaled nicely and centred on the screen.

    I have had some success, but in doing so has created further confusion and problems!

    I can get the image no matter what the size to sit centred scaled enough to see the entire image like this:

    minimumScale = [scrollingView frame].size.width  / [contentView frame].size.width;
    float heightScale = [scrollingView frame].size.height  / [contentView frame].size.height;
    contentHeightOffset = -(heightScale * [scrollingView frame].size.height / 2);
    [scrollingView setMinimumZoomScale: minimumScale];
    [scrollingView setZoomScale: minimumScale];
    scrollingView.contentOffset =  CGPointMake(0, contentHeightOffset); 
    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    if (scale == minimumScale) scrollingView.contentOffset =  CGPointMake(0, contentHeightOffset); 
    This works, but I do not understand that in order for it to do so I have to use the negative of (heightScale * [scrollingView frame].size.height / 2);

    That puzzles me!

    But it works?

    Then I have the added problem that when the view stops scrolling it tries to reset itself to origin 0,0 when it gets sufficiently small enough. So I put code in to test if it hase reached minimum zoom which works....but then you see it bounce back from origin 0,0 to the desired start place?

    Any tips will glady save my sanity!


Share This Page