iOS - 以编程方式添加/删除子视图

and*_*asv 13 iphone objective-c subview uiimageview ios

好吧我想添加一个UIImageView作为子视图,然后在启动画面工作的几秒钟后删除它.我找到了三种不同的方法,但根据Objective-C和Apple,我无法理解哪种方法是最好的方法.

以下是三种不同的方法:

1)在我的MyAppDelegate.h中

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;
    UIImageView *myImageView;
}


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

@end
Run Code Online (Sandbox Code Playgroud)

在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];
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

在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];
 }
Run Code Online (Sandbox Code Playgroud)

3)在第三种方法中:

In my MyAppDelegate.h


@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;

}

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

@end
Run Code Online (Sandbox Code Playgroud)

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

 }
Run Code Online (Sandbox Code Playgroud)

总结一下..第一种方法不使用UIImage的属性只是一个变量,第二种方法使用属性而第三种只是创建UIImage并将其添加为子视图,然后根据其标记将其删除. .

这是正确的方法.我相信这三个选项听起来都是正确的..但是我有什么办法可以遵循.这些选项中的任何一个在内存和性能方面都更好吗?

提前致谢,

安德烈亚斯

小智 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"]; 
Run Code Online (Sandbox Code Playgroud)


Out*_*ndy 6

如果您不打算再次使用该图像,则无需保留指针.此外,如果您使用IBOutlet,您还需要在IB中添加视图.在这个具体的例子中,我会说选项3是最有意义的,特别是考虑到这个选择你可以从一个标准的" 基于视图的应用程序 "模板开始,只需添加关于图像视图的位,剩下的就是其余部分.最后一个观察选择3; 2个消息到窗口;

 [self.window addSubview:myViewController.view];

 [self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

似乎超出任何方法的范围.这可能只是复制和粘贴错误,但请注意它们应位于" didFinishLaunchingWithOptions:"内