2011-09-10 10 views
12

いいえ私はUIImageViewをサブビューとして追加し、スプラッシュ画面の仕方で数秒後に削除したいと考えています。私は3つの異なるアプローチを見つけましたが、どちらがObjective-CとAppleによると最良のアプローチであるのか理解できません。iOS - サブビューをプログラムで追加/削除する

1)私のMyAppDelegate.hにおいて

@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 

    MyViewController *myViewController; 
    UIImageView *myImageView; 
} 


@property (nonatomic, retain) IBOutlet MyViewController *myViewController; 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

とMyAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)]; 

     myImageView.image=[UIImage imageNamed:@"Yoga.png"]; 


    [self.window addSubview:myImageView ]; 
    [self.window bringSubviewToFront:myImageView]; 

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5]; 

    return YES; 
} 

-(void) removeImage 
{ 
    [myImageView removeFromSuperview]; 

    [myImageView release]; 

    [self.window addSubview:myViewController.view]; 
    [self.window makeKeyAndVisible]; 
} 

2)第2のアプローチでは:

以下

は、3つの異なるアプローチであります

In my MyAppDelegate.h 


@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 

    MyViewController *myViewController; 
    UIImageView *myImageView; 
} 

@property (nonatomic, retain) IBOutlet UIImageView *myImageView; 

@property (nonatomic, retain) IBOutlet MyViewController *myViewController; 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

およびMyAppDelegate.m

@synthesize myImageView;第三のアプローチで

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)]; 

     myImageView.image=[UIImage imageNamed:@"Yoga.png"]; 


    [self.window addSubview:myImageView ]; 
    [self.window bringSubviewToFront:myImageView]; 

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5]; 

    return YES; 
} 

-(void) removeImage 
{ 
    [myImageView removeFromSuperview]; 

    [myImageView release]; 

    [self.window addSubview:myViewController.view]; 
    [self.window makeKeyAndVisible]; 
} 

- (void)dealloc 
{ 
    [myViewController release]; 
    [myImageView release]; 
} 

3):

In my MyAppDelegate.h 


@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 

    MyViewController *myViewController; 

} 

@property (nonatomic, retain) IBOutlet MyViewController *myViewController; 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

とMyAppDelegate.mで

..最初のアプローチはUIImageのプロパティを使用しないように要約する

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UIImageView *myImageView =[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,self.window.frame.size.width,self.window.frame.size.height)]; 

    myImageView.image=[UIImage imageNamed:@"Yoga.png"]; 
    myImageView.tag=22;  

    [self.window addSubview:myImageView ]; 

    [myImageView release]; 

    [self.window bringSubviewToFront:myImageView]; 

    [self performSelector:@selector(removeImage) withObject:nil afterDelay:2.5]; 

    return YES; 
} 

-(void) removeImage 
{ 

    for (UIView *subview in [self.view subviews]) { 

    if (subview.tag == 22){ 

     [subview removeFromSuperview]; 

    } 

} 
    [self.window addSubview:myViewController.view]; 
    [self.window makeKeyAndVisible]; 
} 

- (void)dealloc 
{ 
    [myViewController release]; 

} 

変数だけを使用し、2つ目はプロパティを使用し、3つ目はUIImageを作成してサブビューとして追加し、タグに基づいて削除します。

これは正しいアプローチです。私は3つのオプションすべてが正しいと信じています。しかし、私はそれに従うべき方法があります。これらのオプションはメモリとパフォーマンスの面で優れていますか?事前に

おかげで、

アンドレアス

+1

技術的な質問が悪いモバイルデザインで、iPhone HIGによって示唆されているものではありません。 – Nick

+3

質問はスプラッシュ画面ではありません。それは何かを実装するための正しい方法を選択することです..この例は、スプラッシュ画面とはまったく関係のない質問になる可能性があります。それは、サブビューを追加し、後でそれを削除する正しい方法です.. – andreasv

+0

最初のアプローチは正しいようですが、それはアウトレットなので、UIImageViewを初期化する必要はありません、ちょうど遅延時間後にメソッドを起動し、それを削除あなたのviewControllerになることができるsuperViewから –

答えて

6

あなたは再び画像を使用しない場合は、それへのポインタを保持する必要はありません。さらに、IBOutletを使用する場合は、IBでビューを追加する必要があります。この具体的な例では、オプション3が最も効果的だと考えています。特にこの選択肢では、標準の "ビューベースのアプリケーション"テンプレートから始め、画像ビューに関するビットを追加して残りをそのまま残すことができます。しかし、最後の3つの選択肢があります。ウィンドウへの2つのメッセージ。

[self.window addSubview:myViewController.view]; 

[self.window makeKeyAndVisible]; 

任意の方法の範囲外であると表示されます。これは単なるコピー&ペーストエラーですが、「didFinishLaunchingWithOptions

8

のビューのレイヤーに付随するアニメーションを使用することができます。以下のコードは、ビューを消し去りますが、それを削除する他の多くの方法があります。 (あなたがQuartzCoreフレームワークを添付する必要があります)脇人工的にユーザがアプリケーションを使用するようになって遅延スプラッシュスクリーンを持つ

myImageView.layer.opacity = 0.0; 
// this is the state the view will be in after the animation (e.g. invisible) 

CABasicAnimation *theFade; 

theFade = [CABasicAnimation animationwithKeyPath:@"opacity"]; 
theFade.duration = 10.0; 
theFade.fromValue = [NSNumber numberWithFloat:1.0]; // i.e. visible 
theFade.toValue = [NSNumber numberWithFloat:0.0]; // i.e. invisible 
[myImageView.layer addAnimation:theFade forKey:@"animateOpacity"]; 
関連する問題