2011-08-05 2 views
1

12 개의 이미지가 배열에 저장되어 있습니다 ...페이지에서 이미지를 랜덤 화하는 방법 scrollview

이미지를 출력하는 데 사용합니다.

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] 

어떻게 UIView에서 임의의 이미지를 출력 할 수 있습니까? 거기에 12 이미지가 있지만, 내가 응용 프로그램을 실행할 때마다, 응용 프로그램이 임의의 이미지에서 시작됩니다,하지만 난 여전히 이미지를 스크롤 할 수 있습니다. 너희들이 내 질문을 이해하기를 바랍니다.

답변

3

당신은 "셔플"이있는 NSMutableArray 때마다 당신이 그것을 만들 수 있습니다

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

그래서 당신은 다른 순서로있는 UIScrollView 초기화됩니다 때마다.

shuffle은 SDK에 포함되어 있지 않습니다. 샘플 구현을 위해, have a look to this하십시오

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

은 어디든지 당신은 shuffle를 사용하려면 :

@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 

가져 오기 당신에게 카테고리 선언을 포함하는 헤더 파일.

+0

아, 멋지 네요 ... 고마워요.하지만 코드를 보았을 때 셔플이 NSMutableArray에 응답하지 않는다고합니다. – Harvin

+0

경고는 문제가되지 않습니다. 어쨌든, 그것을 제거하는 방법에 대한 내 편집을 참조하십시오. – sergio

+0

이것으로 어떤 성공을 거두었습니까? – sergio

관련 문제