开发者

How to randomize images in a page scrollview

I have 12 images, stored in an array...

I use this to output the image.

scrollView = [[UIScrollView alloc] init];
    CGRect scrollFrame;
    scrollFrame.origin.x = 0;
    scrollFrame.origin.y = 0;  
    scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
    scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;

    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    scrollView.bounces = YES;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.userInteractionEnabled = YES;

    NSMutableArray *slideImages = [[NSMutableArray alloc] init];
    [slideImages addObject:@"KODAK1.png"];
    [slideImages addObject:@"KODAK2.png"];
    [slideImages addObject:@"KODAK3.png"];
    [slideImages addObject:@"KODAK4.png"];
    [slideImages addObject:@"KODAK5.png"];
    [slideImages addObject:@"KODAK6.png"];
    [slideImages addObject:@"KODAK7.png"];
    [slideImages addObject:@"KODAK8.png"];
    [slideImages addObject:@"KODAK9.png"];
    [slideImages addObject:@"KODAK10.png"];
    [slideImages addObject:@"KODAK11.png"];
    [slideImages addObject:@"KODAK12.png"];

    srandom(time(NULL));
    int x = arc4random() % 12;
 for ( int i = 0 ;i<[slideImages count]; i++) {
        //loop this bit
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET, 0 , WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
        [scrollView addSubview:imageView];
开发者_开发技巧        [imageView release];
    }


    [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] +0), HEIGHT_OF_IMAGE)];
    [scrollView setContentOffset:CGPointMake(0, 0)];
    [self.view addSubview:scrollView];
    [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:YES]; 
    [super viewDidLoad]

How can i output random images in the UIView? as in there are 12 images, but each time i run the application, the app will start at a random image, but i will still be able to scroll through the images. i hope you guys understand my question.


You could "shuffle" the NSMutableArray each time you create it:

 NSMutableArray *slideImages = [[NSMutableArray alloc] init];
 ...
 [slideImages shuffle];
 ...

So each time you will be initializing the UIScrollView with a different order.

shuffle is not part of the SDK. For an sample implementation, please have a look to this:

@implementation NSMutableArray (Shuffling)

- (void)shuffle
{

  static BOOL seeded = NO;
  if(!seeded)
  {
    seeded = YES;
    srandom(time(NULL));
  }

    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end

Import the header file containing you category declaration:

@interface NSMutableArray (Shuffling)
   - (void)shuffle;
@end

wherever you want to use shuffle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜