【问题标题】:iOS - adding/removing a subview programmaticallyiOS - 以编程方式添加/删除子视图
【发布时间】:2011-11-14 11:18:02
【问题描述】:

好的,我想添加一个 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 并将其添加为子视图,然后根据删除它它的标签..

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

提前致谢,

安德烈亚斯

【问题讨论】:

  • 技术问题不谈 人为地延迟用户使用您的应用程序的启动屏幕是糟糕的移动设计,也不是 iPhone HIG 所建议的。
  • 问题不在于启动画面。这是关于选择正确的方式来实现某些东西。这个例子可能是一个与启动屏幕完全无关的问题!这是关于添加子视图并稍后将其删除的正确方法..
  • 第一种方法似乎是正确的,但您不需要初始化 UIImageView,因为它是一个 Outlet,只需在您想要的延迟时间后触发该方法并将其从可以是您的 viewController 的 superView 中删除/跨度>

标签: iphone objective-c ios uiimageview subview


【解决方案1】:

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

 [self.window addSubview:myViewController.view];

 [self.window makeKeyAndVisible];

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2017-02-03
      • 1970-01-01
      • 2011-08-22
      • 2016-07-27
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多