更改 ViewDidAppear 上的核心动画持续时间似乎不起作用
我试图让 iPad 启动画面淡出,以在 2 秒内显示我的应用程序的主界面。主界面是我的主视图控制器和视图,位于导航控制器内。
所以我做了以下操作:
UINavigationController 与我的根视图控制器。 根视图控制器的界面已全部布局,最后一步是 CALayer,该 CALayer 具有与覆盖界面的启动屏幕相同的 png。
这个想法是,一旦真正的启动屏幕消失,CALayer 仍然存在。然后我淡出 CAlayer 以显示界面。它有点有效:淡入淡出发生了,但无论我为动画设置多长的时间,它仍然发生得太快。
下面是代码:(logoLayer 是一个 ivar,mainCanvas 是 self.view 中的一个容器视图,我在其中插入了大多数子视图——用于其他屏幕变暗类型的效果。)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
[self setLayerContent:[[NSBundle mainBundle] pathForResource:@"interfaceBackground" ofType:@"png"]];
[self layoutNavigationButtonWithResource:@"button1" glowing:YES forAction:@"biblioteca"];
[self layoutNavigationButtonWithResource:@"button2"glowing:YES forAction:@"portfolio"];
NSString *logoPath = [[NSBundle mainBundle] pathForResource:@"Default-Portrait~ipad" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:logoPath];
logoLayer = [[CALayer layer] retain];
logoLayer.contents = (id)image.CGImage;
logoLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
logoLayer.anchorPoint = CGPointMake(0, 0);
logoLayer.opacity = 1;
[mainCanvas.layer addSublayer:logoLayer];
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(fadeOutLogo) withObject:nil afterDelay:0.1];
}
- (void)fadeOutLogo
{
CABasicAnimation *a = [CABasicAnimation animation];
a.duration = 10;
[logoLayer addAnimation:a forKey:@"opacity"];
[logoLayer setOpacity:0];
}
请注意,我什至延迟了对动画代码的调用案件。我最终得到的值为 10 秒。这对于褪色来说是荒谬的。不过...淡入淡出会在大约 0.2 秒内发生。
有什么想法吗?
I'm trying to have the iPad splash screen fade out to reveal my app's main interface over 2 secs. The main interface is my main view controller and view, inside a navigation controller.
So I did the following:
UINavigationController with my root view controller.
root view controller has its interface all laid out and, as a last step, a CALayer with the same png used for the splash screen covering the interface.
The idea is that once the real splash screen is gone, there's still the CALayer. And then I fade the CAlayer out to reveal the interface. It kind of works: the fade happens, but no matter what duration I set for the animation, it still happens too fast.
Here's the code: (logoLayer is an ivar and mainCanvas is a container view inside self.view, in which I insert most subviews -- for other screen-dim-type effects.)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
[self setLayerContent:[[NSBundle mainBundle] pathForResource:@"interfaceBackground" ofType:@"png"]];
[self layoutNavigationButtonWithResource:@"button1" glowing:YES forAction:@"biblioteca"];
[self layoutNavigationButtonWithResource:@"button2"glowing:YES forAction:@"portfolio"];
NSString *logoPath = [[NSBundle mainBundle] pathForResource:@"Default-Portrait~ipad" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:logoPath];
logoLayer = [[CALayer layer] retain];
logoLayer.contents = (id)image.CGImage;
logoLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
logoLayer.anchorPoint = CGPointMake(0, 0);
logoLayer.opacity = 1;
[mainCanvas.layer addSublayer:logoLayer];
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(fadeOutLogo) withObject:nil afterDelay:0.1];
}
- (void)fadeOutLogo
{
CABasicAnimation *a = [CABasicAnimation animation];
a.duration = 10;
[logoLayer addAnimation:a forKey:@"opacity"];
[logoLayer setOpacity:0];
}
Notice I even delayed the call to the animation code just in case. And I also ended up with a value of 10sec. which is absurd for a fade. Still... the fade happens in about 0.2 secs.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CABasicAnimation 实际上并没有做任何事情,因为你没有给它一个
fromValue
或toValue
;当您设置图层的不透明度时,它只是动画,因为在核心动画图层上设置属性会触发隐式动画,其默认持续时间约为四分之一秒。你想做的是这样的:That CABasicAnimation isn’t actually doing anything, because you’re not giving it a
fromValue
ortoValue
; when you’re setting the layer’s opacity, it’s only animating because setting properties on Core Animation layers triggers an implicit animation, whose default duration is about a quarter of a second. What you want to do is this:或者,在 viewWillAppear: 中,您可以使用 Default-Portrait~ipad.png 实例化一个 UIImageView,将其添加到 self.view,使用 UIView animateWithDuration:animations:completion: 类方法在 2 的时间内将 alpha 值动画化为 0秒,然后在完成块中移除并释放 UIImageView。下面的代码演示了这个想法,使用 UIView 而不是 UIImageView:
Alternatively, in viewWillAppear: you could instantiate a UIImageView with Default-Portrait~ipad.png, add it to self.view, use the UIView animateWithDuration:animations:completion: class method to animate the value of alpha to 0 over a period of 2 seconds, and then remove and release the UIImageView in the completion block. The following code demonstrates the idea, using a UIView instead of a UIImageView: