2014-01-28 135 views
1

我想制作一些带有动画持续时间的通知框。 这些盒子会向下滑动(从顶部开始),直到它们覆盖状态栏和导航栏(0.3s),静止并可见大约2.4秒,然后从屏幕滑回(0.3s)。iOS上的动画持续时间

我粗略地让他们如下:

notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 64)]; 
[UIView animateWithDuration:0.3 delay:2.4 options:UIViewAnimationOptionCurveLinear animations:^{notificationLabel.frame = CGRectMake(0,-64, 320, 64);} completion:nil]; 

这些代码有两个问题。首先,我无法在顶部栏上显示此通知标签的位置。如果我向下滚动,我应该滑动以查看此标签。我只是想看到这个像导航栏。

二,显示和隐藏通知标签不是确切的时间。 如何更改我的代码以解决这些问题?

请让我知道。谢谢。

回答

1

我显示在导航\状态栏的顶部上的通知的方法是通过将其加入到keyWindow像这样:然后,为了动画它您所描述的方式

[[UIApplication sharedApplication].keyWindow addSubview:notificationLabel]; 

notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -64, 320, 64)]; 
[UIView animateWithDuration:0.3 animations:^{ 
    notificationLabel.frame = CGRectMake(0, 0, 320, 64); 
} completion:^(BOOL finished) { 
    [UIView animateWithDuration:0.3 delay:2.4 options:UIViewAnimationOptionCurveLinear animations:^{ 
     notificationLabel.frame = CGRectMake(0, -64, 320, 64); 
    } completion:nil]; 
}]; 

这不是很优雅,但它晚了,这是我可以拿出:)

1

我会建议使用关键帧。这意味着您可以使用相对的开始时间连续添加动画。一个小例子如下:

//Create your label above the screen outside of the field of view. 
    notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -64, 320, 64)]; 

//Add your subview to the window here. Whether its adding it to a subview or to the main window 
// ex. [self.view addSubview:notificationLabel]; 

[UIView animateKeyframesWithDuration:3.0f delay:0.0f options:UIViewKeyframeAnimationOptionAllowUserInteraction animations:^{ 
    [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.3f animations:^{ 
     //Animate the sliding down of your notification here at 0.0 seconds and it lasts 0.3 seconds 
     [notificationLabel setFrame:CGRectMake(0,0, 320, 64)]; 
    }]; 
    [UIView addKeyframeWithRelativeStartTime:0.3f relativeDuration:2.4f animations:^{ 
     //Animate the sliding down of your notification here at 0.3 seconds and it lasts 2.4 seconds 
     //really, you'll do nothing here. so this frame is optional. 
    }]; 
    [UIView addKeyframeWithRelativeStartTime:2.7f relativeDuration:0.3f animations:^{ 
     //Animate the sliding up of your notification here at 0.0 seconds and it lasts 0.3 seconds 
     [notificationLabel setFrame:CGRectMake(0,-64, 320, 64)]; 
    }]; 
} completion:nil];