好的我想添加一个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并将其添加为子视图然后根据它将其删除它的标签..
这是正确的方法。我相信所有三个选项听起来都是正确的..但是我有什么办法可以遵循。这些选项中的任何一个在内存和性能方面都更好吗?
提前致谢,
安德烈亚斯
答案 0 :(得分: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"];
答案 1 :(得分:6)
如果您不打算再次使用该图像,则无需保留指针。此外,如果您使用IBOutlet
,则还需要在IB中添加视图。在这个具体的例子中,我会说选项3是最有意义的,特别是考虑到这个选择你可以从一个标准的“基于视图的应用程序”模板开始,只需添加关于图像视图的位并离开其余的一个人。最后一个观察选择3; 2个消息到窗口;
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
似乎超出任何方法的范围。这可能只是一个复制和粘贴错误,但请注意它们应位于“didFinishLaunchingWithOptions
:”