我想为我的应用程序设置一个随机背景图像,这样每次有人启动应用程序时都会显示一个新的背景图像。
这是迄今为止我在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中尝试过的内容
方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int index = arc4random() % [myImageNames count];
UIColor *background = [[UIColor alloc] initWithPatternImage:[myImageNames objectAtIndex:index]];
self.window.backgroundColor = background;
}
我没有错,但我没有工作...... 有任何想法吗? :)
答案 0 :(得分:1)
而不是委托类将此代码放在主viewcontrller的initWithNibName方法中:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png",@"image4.png", @"image5.png",@"image6.png", nil];
int index = arc4random() % [myImageNames count];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]];
}
return self;
}
答案 1 :(得分:1)
当您从图像
设置背景颜色时,我认为您的代码中缺少某些内容请参阅[myImageNames objectAtIndex:index]
,此代码将返回正确的字符串对象。
因为您只是将string
传递给需要image
的方法。
所以你应该把这段代码改成这个
[UIImage imageNamed:[myImageNames objectAtIndex:index]
。
现在你的代码看起来像这样
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int index = arc4random() % [myImageNames count];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: [myImageNames objectAtIndex:index]]];
self.window.backgroundColor = background;
[background release];
}
答案 2 :(得分:1)
您需要考虑两件事。首先,您需要将UIImage
而不是NSString
传递给initWithPatternImage
。代码应该是:
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int index = arc4random() % [myImageNames count];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: [myImageNames objectAtIndex:index]]];
self.window.backgroundColor = background;
[background release];
另一件事是当iOS实例化主故事板时,它会为你做一些事情。其中之一是分配和初始化self.window
属性,但它还实例化了故事板中包含的主视图控制器,并将其分配给UIWindow
的{{1}}属性。这意味着如果您的主视图控制器创建rootViewController
(几乎每个视图控制器都这样做),它的背景将在视觉上覆盖UIView
背景,除非它被设置为透明。设置透明的UIWindow
可能不是你想要的,顺便说一下,我建议在@Dilip回答之后将背景设置为根视图控制器的视图。
答案 3 :(得分:0)
试试这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int index = arc4random() % [myImageNames count];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]];
self.window.backgroundColor = background;
[background release];
}
答案 4 :(得分:0)
我感谢所有回复的人,谢谢大家!
代码应为:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *myImageNames = [NSMutableArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.png", nil];
int index = arc4random() % [myImageNames count];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:[myImageNames objectAtIndex:index]]];
self.window.backgroundColor = background;
// Override point for customization after application launch.
return YES;
}