在 UIImageView 动画中显示随机图像

发布于 2024-12-17 02:39:48 字数 1874 浏览 0 评论 0原文

在显示动画的正常方式中,我们提供一系列依次显示的图像。比如:

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 秒的间隔。

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

In the normal way for showing animations, we provide a sequence of images which are shown one after the other. Something like:

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

Is there any way I can show a randomly picked image from these four (or any number of) images? Currently I am thinking of something like

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

which is not random at all, but won't look repetitive to most people, especially with only 0.2 seconds separating the current and the next image.

Is there any way I can make it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-12-24 02:39:48
-(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;
}

尝试这样的事情

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

Try something like this

谈场末日恋爱 2024-12-24 02:39:48

人们非常善于观察规律。如果你确实想要重复,那么至少要以素数为基础,因为它们更难发现。

在任何情况下,您都可以自己完成此操作,只需设置一个计时器并在每次计时器计时时随机选择一个新图像。如果您想确保不会连续两次显示相同的图像,最简单的方法是从数组中随机选择一个图像,直到获得不同的图像。如果你想确保在显示其中一张图像两次之前显示所有 4 张图像,然后随机洗牌你的阵列并从前面取出项目,然后当你用完时,再次洗牌(为了避免重复问题,只需继续洗牌直到数组中的第一个条目与前一个数组中的最后一个条目不同)。

People are remarkably good at seeing patterns. If you do want repetition, then at least base it on prime numbers, since those are much harder to spot.

In any case, you could do this yourself simply by setting up a timer and randomly selecting a new image each time the timer ticks. If you want to ensure you don't show the same image twice in a row, the simplest way is to just randomly pick an image from your array until you get a different one. If you want to ensure you show all 4 images before showing one of them twice, then randomly shuffle your array and take items off the front, then when you run out, shuffle it again (and to avoid the duplicate problem, just keep shuffling until the first entry in the array is different than the last entry in the previous array).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文