1

アプリケーションにランダムな背景画像を設定して、誰かがアプリを起動するたびに新しい背景画像が表示されるようにしたいと考えています。

メソッドでこれまでに試したことは次の- (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;
}

エラーは発生しませんが、機能しません...何かアイデアはありますか? :)

4

5 に答える 5

1

デリゲート クラスの代わりに、このコードをメインの viewcontroller の 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;
}
于 2013-02-16T12:43:48.023 に答える
1

考慮すべき点が 2 つあります。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];

もう 1 つは、iOS がメインのストーリーボードをインスタンス化するときに、いくつかのことを行うことです。それらの 1 つは、プロパティの割り当てと初期化ですself.windowが、ストーリーボードに含まれるメイン ビュー コントローラーもインスタンス化し、それをUIWindowrootViewControllerプロパティに割り当てます。これは、メイン ビュー コントローラーが (ほとんどすべてのビュー コントローラーが行うように) を作成する場合、透明に設定されていない限りUIView、その背景が背景を視覚的に上書きすることを意味します。UIWindow透明を設定するのUIViewはあなたが望むものではないかもしれませんし、ちなみに扱いにくいので、@ Dilipの回答に従って、背景をルートView Controllerのビューに設定することをお勧めします。

于 2013-02-16T13:22:20.997 に答える
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];
}
于 2013-02-16T12:53:03.903 に答える
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];
}
于 2013-02-16T12:45:01.170 に答える
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;
}
于 2013-02-19T11:48:43.013 に答える