2011-09-10 117 views
12

好吧我想添加一個UIImageView作爲子視圖,然後在啓動屏幕的方式幾秒鐘後將其刪除。我發現了三種不同的方法,但根據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):

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

- (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]; 

} 

所以總結起來..第一種方法不使用屬性爲的UIImage只有一個變量,第二個使用屬性,第三個只創建UIImage並將其添加爲子視圖,然後根據其標記刪除它。

哪一種是正確的方法。我相信所有三個選項聽起來都是正確的..但有沒有我應該遵循的某種方式。這些選項在內存和性能方面是否更好?

由於提前,

安德烈亞斯

+1

技術問題放一邊有閃屏是人爲地延遲用戶獲得使用你的應用是糟糕的移動設計,是不是有什麼用iPhone HIG建議。 – Nick

+3

這個問題不是關於啓動畫面。這是關於選擇正確的方式來實現的東西..這個例子可能是一個與啓動畫面無關的問題!這是關於正確的方式來添加一個子視圖,然後刪除它.. – andreasv

+0

第一種方法似乎是正確的,但你不需要初始化你的UIImageView,因爲它是一個出口,只是在你想要的延遲時間後觸發該方法,並將其刪除從superView可以是你的viewController –

回答

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"]; 
相關問題