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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7373544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:27:19  来源:igfitidea点击:

iOS - adding/removing a subview programmatically

iphoneobjective-ciosuiimageviewsubview

提问by andreasv

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.

好的,我想添加一个 UIImageView 作为子视图,然后在几秒钟后以启动画面的工作方式将其删除。我找到了三种不同的方法来做到这一点,但我无法理解根据 Objective-C 和 Apple 的说法,哪一种是最好的方法。

Below are the three different approaches:

以下是三种不同的方法:

1) In my MyAppDelegate.h

1) 在我的 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

并在 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:

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

and in MyAppDelegate.m

并在 MyAppDelegate.m 中

@synthesize myImageView;

@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:

3)在第三种方法中:

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

并在 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..

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

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

安德烈亚斯

采纳答案by OutlawAndy

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;

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

 [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:"

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

回答by TCR Dave

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)

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