在 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 秒的间隔。
我有什么办法可以让它变得更好吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情
Try something like this
人们非常善于观察规律。如果你确实想要重复,那么至少要以素数为基础,因为它们更难发现。
在任何情况下,您都可以自己完成此操作,只需设置一个计时器并在每次计时器计时时随机选择一个新图像。如果您想确保不会连续两次显示相同的图像,最简单的方法是从数组中随机选择一个图像,直到获得不同的图像。如果你想确保在显示其中一张图像两次之前显示所有 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).