簡體   English   中英

在ViewDidAppear上更改核心動畫持續時間似乎不起作用

[英]changing Core Animation duration on ViewDidAppear doesn't seem to work

我試圖讓iPad啟動畫面淡出,以便在2秒內顯示我的應用程序的主界面。 主界面是我的主視圖控制器和視圖,在導航控制器內。

所以我做了以下事情:

UINavigationController與我的根視圖控制器。 根視圖控制器的界面全部布局,最后一步,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秒。

有什么想法嗎?

這CABasicAnimation實際上並沒有做任何事情,因為你沒有給它一個fromValuetoValue ; 當您設置圖層的不透明度時,它只是動畫,因為核心動畫圖層上的設置屬性會觸發隱式動畫,其默認持續時間約為四分之一秒。 你想要做的是:

- (void)fadeOutLogo
{
    [CATransaction begin];
    [CATransaction setAnimationDuration:2];
    [logoLayer setOpacity:0];
    [CATransaction commit];
}

或者,在viewWillAppear中:您可以使用Default-Portrait~ipad.png實例化UIImageView,將其添加到self.view,使用UIView animateWithDuration:animations:completion:class方法將alpha的值設置為0的值,為期2秒,然后在完成塊中刪除並釋放UIImageView。 下面的代碼演示了這個想法,使用UIView而不是UIImageView:

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear: animated];
  UIView __block *fadeView = [[UIView alloc] 
  initWithFrame: CGRectMake(0, 0, 320, 460)];
  fadeView.backgroundColor = [UIColor blackColor];
  [self.view addSubview: fadeView];
  [UIView animateWithDuration: 2 animations:^{
    fadeView.alpha = 0;
  } completion:^(BOOL finished) {
  fadeView = nil;
  }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM