UIScrollView - after zoom inner content auto moves to top left corner of UIScrollView

Discussion in 'iPhone/iPad Programming' started by springframework, Aug 5, 2008.

  1. macrumors member

    Joined:
    Mar 31, 2008
    #1
    I am trying to do a zoom in, just a basic one for some lines and labels inside a UIView.

    it works good but the problem im having now is when you zoom in or out. right after you zoom ... the inner content scrolls to have its top left corner at the (0,0) position of the UISCrollView.

    I need to have this stopped, unless the zoom in makes the inner content outside of the allowable scroll area.
     
  2. macrumors member

    Joined:
    Jul 28, 2008
    #2
    Hum, I'm also using a UIScrollView and it works ok. Are you doing anything special in viewForScrolling or didEndZooming ?
     
  3. thread starter macrumors member

    Joined:
    Mar 31, 2008
    #3
    can u post your code?
     
  4. thread starter macrumors member

    Joined:
    Mar 31, 2008
    #4
    here is the code im using. (innerview is a class variable that holds stuff like labels/images/lines ...) innerview is what should be zoomed/panned.


    PHP:
        //set up the scroll view for the editable content screen
        
    CGRect aframe CGRectMake(040320100);
        
    UIScrollView *scrollView = [[UIScrollView allocinitWithFrame:aframe];
        
    scrollView.contentSize CGSizeMake(600200);
        
    scrollView.backgroundColor = [UIColor clearColor];
        
    scrollView.maximumZoomScale 2;
        
    scrollView.minimumZoomScale 0.25;
        
    scrollView.scrollEnabled YES;
        
    scrollView.scrollsToTop NO;
        
    scrollView.autoresizingMask UIViewAutoresizingFlexibleHeight;
        
    scrollView.delegate self;
        [
    scrollView addSubview:innerview];
        [
    window addSubview:scrollView];


    these aree my delegates

    PHP:
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        
    NSLog(@"- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{");
        return 
    innerview;
    }

    - (
    void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
        
    NSLog(@"- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale %@, %@, %f"scrollViewviewscale);
        
    //properly sets the scrolling bounds.
        
    scrollView.contentSize CGSizeMake(600*scale5*24*scale);
    }


    so ... why isnt this working?

    this is a simple test application and so i did everything inside the appdelegate class, could that affect things?
     
  5. thread starter macrumors member

    Joined:
    Mar 31, 2008
    #5
    ok i figured out what was wrong.

    i was doing this:

    PHP:
    innerview = [[UIView allocinitWithFrame:CGRectZero];
    is seems the frame needs to have its bounds set.
     
  6. macrumors newbie

    Joined:
    Aug 21, 2008
    #6
    hi, i've got the same problem, allthough i defined the contentView's frame like this:

    scrollView.m:
    PHP:
    - (id)initWithFrame:(CGRect)frame 
    {
        if (
    self = [super initWithFrame:frame]) 
        {
            
    contentView = [[UIView allocinitWithFrame:frame];
            
            
    //content image
            
    UIImagebg = [UIImage imageNamed:@"map_bg.png"];
            
    UIImageViewbgView = [[UIImageView allocinitWithImage:bg];
            [
    contentView addSubview:bgView];
            [
    bgView release];
            [
    bg release];
        }
    }
    which is called by the scrollView's controller like that:

    PHP:
    CGRect applicationFrame = [self.view bounds];
    mapImageViewmapImage= [[mapImageView allocinitWithFrame:applicationFrame];
    the rest of the code is quite identical to yours. anyway, the displayed content always moves to the upper left corner of the contentView after zooming events.

    if i log the framesize using NSLog i get the correct result of 300x480
    could it be a problem, that the contentView's frame resizes on zooming events? if i zoom out for example, the frame changes to 203x324

    how can i stop the view moving to the upper left corner and make it stay on the same place?
     
  7. thread starter macrumors member

    Joined:
    Mar 31, 2008
    #7
    you would have to post more code.

    your code that you posted doesn't even have the word UIScrollView in it.

    I would have to see it all to help you out.
     
  8. macrumors newbie

    Joined:
    Aug 21, 2008
    #8
    ok, here's the my code, would be great if you could help me!
    for detail: i'm planning to show an image of a map, which the user should be able to scroll AND zoom properly.

    mapImageView.h:
    PHP:
    #import <UIKit/UIKit.h>

    @interface mapImageView UIScrollView <UIScrollViewDelegate>
    {
        
    UIViewcontentView;
    }

    @
    property(nonatomicretainUIViewcontentView;

    @
    end
    mapImageView.m:
    PHP:
    #import "mapImageView.h"

    @implementation mapImageView

    #define MAPWIDTH 1688
    #define MAPHEIGHT 1125

    @synthesize contentView;

    - (
    id)initWithFrame:(CGRect)frame 
    {
        if (
    self = [super initWithFrame:frame]) 
        {
            
    contentView = [[UIView allocinitWithFrame:CGRectMake(00frame.size.widthframe.size.height)];
            
            
    //background imageLayer mountains
            
    UIImagebg = [UIImage imageNamed:@"map_bg.png"];
            
    UIImageViewbgView = [[UIImageView allocinitWithImage:bg];
            [
    contentView addSubview:bgView];
            [
    bgView release];
            [
    bg release];    
            [
    self addSubview:contentView];
                
            
    self.contentSize CGSizeMake(MAPWIDTHMAPHEIGHT);
            
    self.scrollEnabled TRUE;
            
    self.bounces FALSE;
            
    //self.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0);
            
    self.bouncesZoom FALSE;
            
    self.minimumZoomScale 0.285;
            
    self.maximumZoomScale 1.0;
            
    self.delegate self;
            [
    self scrollRectToVisible:CGRectMake(2500480100animated:NO];
        }
        return 
    self;
    }

    - (
    UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
    {
        return 
    contentView;
    }

    - (
    void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale 
    {
        
    //set the scrolling bounds
        
    self.contentSize CGSizeMake(MAPWIDTH*scaleMAPHEIGHT*scale);
        
    NSLog(@"scale: %f"scale);
        
    NSLog(@"content width: %f"self.contentSize.width);
        
    NSLog(@"content height: %f"self.contentSize.height);
        
    NSLog(@"frame width %f"contentView.frame.size.width);
        
    NSLog(@"frame height %f"contentView.frame.size.height);
    }

    - (
    void)dealloc 
    {
        [
    contentView release];
        [
    super dealloc];
    }

    @
    end
    mapImageViewController.m:
    PHP:
    #import "mapImageViewController.h"
    #import "mapImageView.h"

    @implementation mapImageViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    {
        if (
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
        {
            
    CGRect applicationFrame CGRectMake(00480300);
            
    mapImageViewmapImage= [[mapImageView allocinitWithFrame:applicationFrame];
            
    self.view mapImage;
        }
        return 
    self;
    }

    @
    end
    maybe you can tell me, how to make the uiscrollview "remember" its position after zooming operations...
     
  9. macrumors member

    Joined:
    Jul 28, 2008
    #9
    I think you're doing it the other way round : you actually want an image to be zoomed-out. Not sure if that is even supported by UIScrollView.

    Also, you set the contentView to be of size frame.size, but then tell the scroll view that your contentView has a size of MAPWIDTH * MAPHEIGHT. I think you should set your contentView's frame to be 0,0,MAPWIDTH,MAPHEIGHT. Changing the contentSize in scrollViewDidEndZooming:withView:atScale: is also a bad idea, AFAIK.

    The problem with your setup is that the map will appear "zoomed-in" by default, and as far as I can tell there is no way to programmatically set the zoom level of a scroll view (but I might have missed something, as this is clearly something one might want to do so it's hard to believe Apple forgot it ... :confused:).
     
  10. macrumors newbie

    Joined:
    Aug 21, 2008
    #10
    thank you!! that works! i set the UIScrollView's framesize to MAPWIDTHxMAPHEIGHT and now it's no longer scrolling to the upper left corner!

    it's true that my image is initially shown at the highest zoom level (that's where map interaction mainly happens) and the user is able to zoom out just to get an overview of the context.
    the other way round, it would be more logic, but with a zoom level > 1 the bitmap pixels are visible quite clearly...
    unfortunately i couldn't find a possibility for setting the zoom level programatically either
     

Share This Page