1

I want to add infinite scrolling images and there is no end for images scrolling:

const CGFloat kScrollObjHeight  = 200.0;
const CGFloat kScrollObjWidth   = 320.0;
const NSUInteger kNumImages     = 17;

- (void)layoutScrollImages
{
    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 = frame;
            curXLoc += (kScrollObjWidth);
        }
    }

    [scrollview1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollview1 bounds].size.height)];
}

-(void)viewDidLoad
{
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
    [scrollview1 setBackgroundColor:[UIColor blackColor]];
    [scrollview1 setCanCancelContentTouches:NO];
    scrollview1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollview1.clipsToBounds = YES;
    scrollview1.scrollEnabled = YES;
    scrollview1.pagingEnabled = YES;

    for(int i=0;i< kNumImages;i++)
    {
        NSString *imgName=[[NSString alloc] initWithFormat:@"head%d.png",i];
        UIImage *img=[UIImage imageNamed:imgName];
        UIImageView *imageView=[[UIImageView alloc] initWithImage:img];

        [imageView setFrame:CGRectMake(i* kScrollObjWidth, 0, kScrollObjWidth,kScrollObjHeight)];
        [scrollview1 addSubview:imageView];
        [imageView release];
        [imgName release];
    }

    scrollview1.contentSize= CGSizeMake(kScrollObjWidth* kNumImages, kScrollObjHeight);

    [self layoutScrollImages];

    [super viewDidLoad];   
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • what do u want these lines to do NSString *imgName=[[NSString alloc] initWithFormat:@"head1.png",i]; and [self layoutScrollImages] – Saraswati Jun 11 '12 at 11:22
  • sorry mam this my last doubt,did u understand the question? if we use only one images its repeating .Here i'm using 5 different images like[(a,b,c,d,e)this want to be a infinite images as (a,b,c,d,e,a,b,c,d,e,.... ] thats only not repeating for me..please help me. – Sangeeth Rajan Jun 12 '12 at 04:17

2 Answers2

1

Just make a simple new project with .h file as

#import <UIKit/UIKit.h>

@interface scrollableImageViewController : UIViewController {

IBOutlet UIScrollView *sview;

}

@end

and the .m file with this method

- (void)viewDidLoad {
[super viewDidLoad];

for(int i=0;i<10;i++)
{
    NSString *name=[[NSString alloc] initWithFormat:@"images%d.png",i];
    NSLog(@"%@",name);
    [name    release];
    UIImage *img=[UIImage imageNamed:@"img.png"];
    UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
    [imageView setFrame:CGRectMake(i*57, 0, 57, 57)];
    [sview addSubview:imageView];

    [imageView release];
}
sview.contentSize= CGSizeMake(57*10, 57);
}

and check the result

Saraswati
  • 1,516
  • 1
  • 14
  • 21
0

You can check out Apples sample code PhotoScroller.
It's the code they introduced at WWDC 11 in Session 104 - Advanced ScrollView Techniques. You can watch this video (and the corresponding PDF) for free if you are a registered Apple developer.

Pfitz
  • 7,336
  • 4
  • 38
  • 51