gpt4 book ai didi

ios - 为什么 didFinishLaunchingWithOptions 中的动画失败了?

转载 作者:行者123 更新时间:2023-11-29 11:12:59 25 4
gpt4 key购买 nike

我只是想在 didFinishLaunchingWithOptions 中显示带有动画的 View ,我的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

CGRect finalRect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);

UIImage *image = [UIImage imageNamed:@"122.jpg"];

UIButton* pic = [[UIButton alloc] initWithFrame:finalRect];
[pic setBackgroundImage:image forState:UIControlStateNormal];
[pic setHidden:true];
pic.center = CGPointMake(finalRect.size.width / 2, finalRect.size.height / 2);


UIViewController* controller = [[UIViewController alloc] init];
[controller setView:pic];

[self.window setRootViewController:controller];
[self.window makeKeyAndVisible];


[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.5];
[pic setHidden:false];
[UIView commitAnimations];

return YES;
}

但是动画根本不起作用。带有图片的 subview 突然出现在屏幕上。但是,如果我使用按钮触发动画代码, View 将按预期显示动画。 didFinishLaunchingWithOptions 有什么限制吗?

附言:

这个问题通过将动画代码移动到 Controller 的 viewDidAppear 过程中解决,就像下面 rokjarc 所说的那样。

但是还有另一种解决方案,通过在从 didFinishLaunchingWithOptions 调用的延迟例程中执行动画代码,例如:

[self performSelector:@selector(executeAnimation) withObject:nil afterDelay:1];

最佳答案

您的按钮被 controller.view 覆盖了这也加载在 window.view 中在你的 pic 之上.

您可以通过将代码移至 controller::viewDidAppear 来解决此问题.

如果您决定采用这种方式,请不要忘记将按钮添加到 controller.view而不是 appDelegate.window .

因为看起来您只想在应用程序启动时显示此动画,您可以设置为 Controller 添加一个名为 showAnimation 的 BOOL 属性。将此属性设置为 YESdidFinishLaunchingWithOptions:NOcontroller::viewDidAppear 的末尾.

这样您就可以有条件地 ( if (self.showAnimation)... ) 只显示一次所需的动画(在 controller::viewDidAppear 中)。

关于ios - 为什么 didFinishLaunchingWithOptions 中的动画失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com