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

发布于 2024-12-03 16:20:59 字数 3921 浏览 0 评论 0原文

好吧,我想添加一个 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

和 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并添加它作为子视图,然后根据其标签将其删除。

这是正确的方法。我相信所有三个选项听起来都是正确的。但是我应该遵循什么特定的方法。这些选项在内存和性能方面是否更好?

预先感谢,

安德烈亚斯

Ok I want to add a UIImageView as a subview and then remove it after a couple of seconds in the way a splash screen works. I found three different approaches to do it but I can not understand which one is the best approach according to Objective-C and Apple.

Below are the three different approaches:

1)
In my MyAppDelegate.h

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;
    UIImageView *myImageView;
}


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

@end

and in 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 the second approach:

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

and in 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 the third approach:

In my MyAppDelegate.h


@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    MyViewController *myViewController;

}

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

@end

and in 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];

 }

So to sum up.. The first approach does not use a property for the UIImage only a variable, the second one uses a property and the third one just creates the UIImage and adds it as a subview and then removes it based on its tag..

Which is the right approach to follow..I believe that all three options sound right.. But is there any certain way I should follow. Is any of these options better in terms of memory and performance?

Thanks in advance,

Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-10 16:21:00

您可以使用附加到视图层的动画。下面的代码使视图淡出 - 但还有许多其他方法可以删除它。 (需要附加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"]; 

You could use an animation attached to the view's layer. Code below fades the view out - but there are many other ways to remove it. (you need to attach the QuartzCore framework)

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"]; 
小…楫夜泊 2024-12-10 16:21:00

如果您不打算再次使用该图像,则无需保留指向它的指针。此外,如果您使用IBOutlet,您还需要在IB中添加视图。在这个具体示例中,我想说选项 3 最有意义,特别是考虑到通过此选择,您可以从标准的“基于视图的应用程序”模板开始,只需添加有关图像视图的位即可剩下的就独自一人吧。不过,最后一个观察结果是选择 3;发送到窗口的 2 条消息;

 [self.window addSubview:myViewController.view];

 [self.window makeKeyAndVisible];

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

If you are not going to use the image again, there is no need to keep a pointer to it. Further, if you use IBOutlet, you need to add the view in IB as well. In this specific example I would say option 3 makes the most sence, especially considering that with this choice you can began with a standard "view based application" template and just add the bits about the image view and leave the rest alone. One last observation of choice 3 though; the 2 messages to window;

 [self.window addSubview:myViewController.view];

 [self.window makeKeyAndVisible];

Appear to be outside the scope of any method. This is likely just a copy and paste error, but make note that they should located within "didFinishLaunchingWithOptions:"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文