![](/img/trans.png)
[英]iOS - removing subview that added programmatically and printing date in label
[英]iOS - adding/removing a subview programmatically
好吧我想添加一个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并将其添加为子视图,然后根据其标记将其删除。 。
这是正确的方法。我相信这三个选项听起来都是正确的..但是我有什么办法可以遵循。 这些选项中的任何一个在内存和性能方面都更好吗?
提前致谢,
安德烈亚斯
您可以使用附加到视图图层的动画。 下面的代码淡出了视图 - 但还有许多其他方法可以删除它。 (你需要附加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"];
如果您不打算再次使用该图像,则无需保留指针。 此外,如果您使用IBOutlet
,您还需要在IB中添加视图。 在这个具体的例子中,我会说选项3是最有意义的,特别是考虑到这个选择你可以从一个标准的“ 基于视图的应用程序 ”模板开始,只需添加关于图像视图的位,剩下的就是其余部分。 最后一个观察选择3; 2个消息到窗口;
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
似乎超出任何方法的范围。 这可能只是一个复制和粘贴错误,但请注意它们应位于“ didFinishLaunchingWithOptions
:”中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.