我有一个应用程序,你有3个按钮,如果按下按钮,三个按钮的按钮图像会改变,但是按钮2和按钮3可能与按钮1的按钮图像不同,按钮3可能没有相同的图像按钮2
我试过这个但是没有用
NSInteger imageIndex1 = arc4random_uniform(images.count);
NSInteger imageIndex2 = arc4random_uniform(images.count);
NSInteger imageIndex3 = arc4random_uniform(images.count);
if (imageIndex1 == imageIndex2 ) {
imageIndex2 = arc4random_uniform(images.count);
}
else if (imageIndex1 == imageIndex3 ) {
imageIndex3 = arc4random_uniform(images.count);
}
else if (imageIndex2 == imageIndex3 ) {
imageIndex3 = arc4random_uniform(images.count);
}
else
[self.picture1 setImage:images[imageIndex1] forState:UIControlStateNormal];
[self.picture2 setImage:images[imageIndex2] forState:UIControlStateNormal];
[self.picture3 setImage:images[imageIndex3] forState:UIControlStateNormal];
imageIndex1 = imageIndex2;
imageIndex1 = imageIndex3;
imageIndex2 = imageIndex3;
答案 0 :(得分:0)
在这里你设置了self.picture1
图像在else部分,所以如果第一个任何条件为真,那么self.picture1
图像将不会设置,所以你面对的这个问题也为{{{}之外的每个按钮添加了setImage的代码1}}部分,下面的代码
else
答案 1 :(得分:0)
试试这个......
if (imageIndex2 == imageIndex1 ) {
imageIndex2 = arc4random_uniform(images.count);
}
if (imageIndex3 == imageIndex1 ) {
imageIndex3 = arc4random_uniform(images.count);
}
[self.picture1 setImage:images[imageIndex1] forState:UIControlStateNormal];
[self.picture2 setImage:images[imageIndex2] forState:UIControlStateNormal];
[self.picture3 setImage:images[imageIndex3] forState:UIControlStateNormal];
imageIndex1 = imageIndex2;
imageInde
x1 = imageIndex3;
答案 2 :(得分:0)
试试这个:
NSInteger imageIndex1 = arc4random_uniform(images.count);
NSInteger imageIndex2 = arc4random_uniform(images.count);
NSInteger imageIndex3 = arc4random_uniform(images.count);
while (imageIndex2 == imageIndex1 ) {
imageIndex2 = arc4random_uniform(images.count);
}
while (imageIndex3 == imageIndex1 ) {
imageIndex3 = arc4random_uniform(images.count);
}
[self.picture1 setImage:images[imageIndex1] forState:UIControlStateNormal];
[self.picture2 setImage:images[imageIndex2] forState:UIControlStateNormal];
[self.picture3 setImage:images[imageIndex3] forState:UIControlStateNormal];
答案 3 :(得分:0)
我想我找到了解决方案。 这是现在的代码:
Boolean p1 =true;
Boolean p2 = true;
Boolean p3 = true;
NSInteger imageIndex1 = arc4random_uniform(images.count);
NSInteger imageIndex2 = arc4random_uniform(images.count);
NSInteger imageIndex3 = arc4random_uniform(images.count);
if (imageIndex1 == imageIndex2 ) {
p1 = false;
imageIndex2 = arc4random_uniform(images.count);
}
if(imageIndex1 == imageIndex3 ) {
p2 = false;
imageIndex1 = arc4random_uniform(images.count);
}
if (imageIndex2 == imageIndex3 ) {
p3 = false;
imageIndex3 = arc4random_uniform(images.count);
}
if(p1 == true && p2 == true && p3 == true) {
[self.picture1 setImage:images[imageIndex1] forState:UIControlStateNormal];
[self.picture2 setImage:images[imageIndex2] forState:UIControlStateNormal];
[self.picture3 setImage:images[imageIndex3] forState:UIControlStateNormal];
}
答案 4 :(得分:0)
看起来其他副本已被关闭为重复。以下是该问题的答案:问题的目标是随机更改按钮上的图像。但问题是在图像数组中选择一个随机索引有时会产生当前索引(它是随机的,所以这发生在1 / array.count概率)。这是一种排除给定指数的方法......
- (NSUInteger)randomUnsignedLessThan:(NSInteger)max excluding:(NSUInteger)exclude {
NSInteger firstTry = -1;
while (firstTry == exclude) firstTry = arc4random() % max;
return firstTry;
}
请注意,此方法始终会调用arc4random一次,并且需要1 + N次调用,概率为1 / max ^ N,因此对于低范围和高性能要求,您可能会考虑使用不同的算法来排除一个索引