Scrollview help

Discussion in 'iPhone/iPad Programming' started by celric, Jul 19, 2011.

  1. celric, Jul 19, 2011
    Last edited by a moderator: Jul 19, 2011

    macrumors newbie

    Joined:
    Jul 18, 2011
    #1
    Hi all,

    Sorry im a newbie to ios. Is there any method in scrollview that will be called when user start scrolling the view? I've gone through some samples but most of them are using pagecontrol to do the. Basically what i want to achieve is that i got 50 images i put into the scrollview, and each image should have a label below. so when scroll on the scrollview i want to change the label text as well.

    i have a scrollview in my secondviewcontroller.h
    Code:
    IBOutlet UIScrollView *scrollView1;
    
    Here is how i create image & make subviews in viewdidload of my secondviewcontroller.m
    Code:
    const CGFloat kScrollObjHeight	= 800;
    const CGFloat kScrollObjWidth	= 600;
    const NSUInteger kNumImages	= 50;
    
    - (void)viewDidLoad {
            [scrollView1 setCanCancelContentTouches:NO];
    	scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    	scrollView1.clipsToBounds = YES;
    	scrollView1.scrollEnabled = YES;
    	scrollView1.pagingEnabled = YES;
    
            NSUInteger i;
    	for (i = 1; i <= kNumImages; i++)
    	{
    		NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
    		UIImage *image = [UIImage imageNamed:imageName];
    		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            
    		CGRect rect = imageView.frame;
    		rect.size.height = kScrollObjHeight;
    		rect.size.width = kScrollObjWidth;
    		imageView.frame = rect;
    		imageView.tag = i;	// tag images for later use when we place them in serial fashion
    		[scrollView1 addSubview:imageView];
                    [imageView release];
                    [self layoutScrollImages:0];
            }
    }
    
    and here is where i split out the images.
    Code:
    - (void)layoutScrollImages:(int)page
    {
        if (page < 0)
            return;
        if (page >= kNumImages)
            return;
    
    	UIImageView *view = nil;
    	NSArray *subviews = [scrollView1 subviews];
    
    	CGFloat curXLoc = 0;
            
    	for (view in subviews)
    	{
    		if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    		{
    			CGRect frame = view.frame;
    			frame.origin = CGPointMake(curXLoc, 0);
    			view.frame = scrollView1.frame;
    			curXLoc += (kScrollObjWidth);     
    		}
    	}
    }
    
    I only have a ScrollView & a Label in my .xib, but i want to change the text when the viewchange. what i got now just able to scroll the view but got no idea how to change the label text as viewchange. Any help would be greatly appreciated. Thanks in advance.

    Here is how i create image & make subviews in viewdidload of my secondviewcontroller.m
    Code:
    const CGFloat kScrollObjHeight	= 800;
    const CGFloat kScrollObjWidth	= 600;
    const NSUInteger kNumImages	= 50;
    
    - (void)viewDidLoad {
            [scrollView1 setCanCancelContentTouches:NO];
    	scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    	scrollView1.clipsToBounds = YES;
    	scrollView1.scrollEnabled = YES;
    	scrollView1.pagingEnabled = YES;
    
            NSUInteger i;
    	for (i = 1; i <= kNumImages; i++)
    	{
    		NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
    		UIImage *image = [UIImage imageNamed:imageName];
    		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            
    		CGRect rect = imageView.frame;
    		rect.size.height = kScrollObjHeight;
    		rect.size.width = kScrollObjWidth;
    		imageView.frame = rect;
    		imageView.tag = i;	// tag images for later use when we place them in serial fashion
    		[scrollView1 addSubview:imageView];
                    [imageView release];
           }
           [COLOR="Red"][self layoutScrollImages:0];[/COLOR]
    }
    
    misplacement of the line just now.
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    UIScrollView has a delegate protocol. Look at scrollViewDidScroll.
     
  3. thread starter macrumors newbie

    Joined:
    Jul 18, 2011
    #3
    I think i found the solution, thanks for the info! appreciate it :)
     

Share This Page