PDA

View Full Version : Scrollview help




celric
Jul 19, 2011, 04:33 AM
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

IBOutlet UIScrollView *scrollView1;


Here is how i create image & make subviews in viewdidload of my secondviewcontroller.m

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.

- (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

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];
}

misplacement of the line just now.



PhoneyDeveloper
Jul 19, 2011, 07:09 AM
UIScrollView has a delegate protocol. Look at scrollViewDidScroll.

celric
Jul 19, 2011, 09:50 AM
UIScrollView has a delegate protocol. Look at scrollViewDidScroll.

I think i found the solution, thanks for the info! appreciate it :)