我想随机更改UIButton
的背景图像。我有一些图像(5),我想随意切换为UIButton
的背景图像。我怎样才能实现这个?请帮我解决这个问题。随机更改UIButton背景图像
1
A
回答
1
如何像这样
NSInteger randomNumber = arc4random_uniform(5); // random number, either 0,1,2,3 or 4
UIImage *randomImage = [UIImage imageNamed:[NSString stringWithFormat:@"img%u.png",randomNumber]];
//UIButton *myButton;
[myButton setBackgroundImage:randomImage forState:UIControlStateNormal];
替代的解决方案,也许更容易让你了解
NSInteger randomNumber = arc4random_uniform(5); // random number, either 0,1,2,3 or 4
UIImage *randomImage;
switch (randomNumber) {
case 0:
randomImage = [UIImage imageNamed:@"img0.png"];
break;
case 1:
randomImage = [UIImage imageNamed:@"img1.png"];
break;
case 2:
randomImage = [UIImage imageNamed:@"img2.png"];
break;
case 3:
randomImage = [UIImage imageNamed:@"img3.png"];
break;
case 4:
randomImage = [UIImage imageNamed:@"img4.png"];
break;
default:
break;
}
//UIButton *myButton;
[myButton setBackgroundImage:randomImage forState:UIControlStateNormal];
备查;你不应该问问你或多或少要求某人为你解决问题。包括你已经试过的东西,即使你只有伪代码。
0
试试这个
//give Your Image name is P0.png, P1.png, P2.png,P3.png, P4.png
int r = arc4random() % 5;
NSString *strImg=[NSString stringWithFormat:@"P%@.png",r];
UIImage *Image;
Image = [UIImage imageNamed:strImg];
[YourButton setBackgroundImage:Image forState:UIControlStateNormal];
0
//Create Array Of Images Name
NSMutableArray *arrayOfImagesName=[[NSMutableArray alloc]initWithObjects:@"img1.png",@"img2.png",@"img3.png",nil];
//choose random image name from array
int rndNumber=arc4random() % [arrayOfImagesName count];
//set image to the button
[myButton setBackgroundImage:[UIImage imageNamed:[arrayOfImagesName objectAtIndex:rndNumber]] forState:UIControlStateNormal];
0
创建静态int变量设置它的值设置为0开始。当按钮点击发生时,将该变量的值增加1,并将该变量作为参数传递给切换大小写。编写代码来更改每种情况下的图像,并在静态变量的计数达到5时将其重置为0。
// write this at top of implementation file
static int count = 0;
//然后点击链接
{
UIImage *image;
count++;
switch(count)
{
case 1:
image = [UIImage imageNamed:@"pic1.png"];
break;
case 2:
image = [UIImage imageNamed:@"pic2.png"];
break;
case 3:
image = [UIImage imageNamed:@"pic3.png"];
break;
case 4:
image = [UIImage imageNamed:@"pic4.png"];
break;
case 5:
image = [UIImage imageNamed:@"pic5.png"];
count = 0;
break;
}
[button setBackgroundImage: image forState:UIControlStateNormal];
}
相关问题
- 1. 随机图像背景更改PHP CSS
- 2. Javascript随机背景图像更改onload
- 3. 与更改UIButton背景图像问题
- 4. UIButton背景图像点击更改
- 5. 如何更改UIButton的背景图像?
- 6. 随机背景图像CSS3
- 7. 如何更改UIButton中的UIButton背景图像点击ios
- 8. 在鼠标上随机更改背景图像
- 9. 通过使用随机数组更改按钮背景图像
- 10. 在Android中随机更改背景图像
- 11. 在Android中随机更改背景图像
- 12. jQuery随机更改背景图像到几个div
- 13. 更改UIButton背景颜色
- 14. UIButton背景更改点击
- 15. 动画UIButton背景图像
- 16. UIButton背景图像不变
- 17. UIButton的图像和背景
- 18. viewflipper应该随机更改背景
- 19. 随机更改android布局背景
- 20. 更改图像背景
- 21. jquery更改背景图像
- 22. onClick更改背景图像
- 23. DIV上的随机背景图像
- 24. UITableviewCell中的随机背景图像
- 25. 随机背景图像不加载
- 26. 随机图像背景上的链接
- 27. 分配随机背景图像面板
- 28. 刷新时的随机背景图像
- 29. 背景图像丢失随机
- 30. 随机背景图像以JavaScript启动
'[BTN了setBackgroundImage:按钮画面forState:UIControlStateNormal]'设置使用这一点,但什么是你在找什么逻辑? – Buntylm
其实我随意移动按钮,我想随机更改按钮背景图像....当我将点击butoon我需要增加ivalue取决于图像我会改变我的价值增加我如何执行dis .. – Balaji