2011-11-19 99 views
1

在显示动画的常规方式中,我们提供了一个接一个显示的图像序列。喜欢的东西:在UIImageView动画中显示随机图像

NSArray *images1 = [[NSArray alloc] initWithObjects: 
            [UIImage imageNamed:@"img1.png"], 
            [UIImage imageNamed:@"img2.png"], 
            [UIImage imageNamed:@"img3.png"], 
            [UIImage imageNamed:@"img4.png"], 
            nil]; 

     images1=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 256.0, 256.0)]; 
     images1.transform = rotateTransform1; 
     images1.animationImages = myDustImages1; 
     images1.animationDuration = 0.2; // seconds 
     images1.animationRepeatCount = 0; // 0 = loops forever 
     [images1 startAnimating]; 

有什么办法,我可以告诉从这四个(或任意数量的)图像时随机挑选的形象?目前,我想到的是这样

NSArray *images1 = [[NSArray alloc] initWithObjects: 
            [UIImage imageNamed:@"img1.png"], 
            [UIImage imageNamed:@"img2.png"], 
            [UIImage imageNamed:@"img3.png"], 
            [UIImage imageNamed:@"img4.png"], 

            [UIImage imageNamed:@"img2.png"], 
            [UIImage imageNamed:@"img3.png"], 
            [UIImage imageNamed:@"img4.png"], 
            [UIImage imageNamed:@"img1.png"], 

            [UIImage imageNamed:@"img3.png"], 
            [UIImage imageNamed:@"img4.png"], 
            [UIImage imageNamed:@"img2.png"], 
            [UIImage imageNamed:@"img1.png"], 
            nil];.......... 

这是随机可言,但不会重复看大多数人,尤其是只有0.2秒分离当前和下一个图像。

有什么办法可以让它变得更好吗?

回答

3
-(NSArray *)shuffledArray 
{ 

NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]]; 

NSMutableArray *copy = [self mutableCopy]; 
while ([copy count] > 0) 
{ 
    int index = arc4random() % [copy count]; 
    id objectToMove = [copy objectAtIndex:index]; 
    [array addObject:objectToMove]; 
    [copy removeObjectAtIndex:index]; 
} 

    [copy release]; 
    return array; 
} 

尝试这样的事情

3

人们非常擅长看图案。如果你确实需要重复,那么至少要以素数为基础,因为那些更难以发现。

在任何情况下,您都可以通过设置计时器并每次计时器滴答时随机选择一个新图像来完成此操作。如果你想确保你不会连续两次显示相同的图像,最简单的方法是从你的数组中随机选择一个图像,直到你得到一个不同的图像。如果您希望确保在显示其中一张图片两次之前显示所有4张图像,请随机随机洗牌并从前面取下物品,然后在用完时再洗牌(并避免重复出现的问题,直到洗牌结束数组中的第一个条目与前一个数组中的最后一个条目不同)。