动画状态栏iOS 7

时间:2013-12-17 06:16:28

标签: ios objective-c xcode animation statusbar

我正在尝试(努力)为我的应用程序实现类似于错误消息的显示(在某种程度上,状态栏上的滑动错误消息向下滑动)在iOS7中。我无法为我的生活让我的动画正常工作。这是我做动画的方法

- (void)animateHeaderViewWithText:(NSString *)text {

    //add header views
    [self.headerView addSubview:self.headerLabel];
    [self.navigationController.view addSubview:self.headerView];

    //Hide the Status Bar
    statusBarHidden = TRUE;
    [self setNeedsStatusBarAppearanceUpdate];

    self.headerLabel.text = text;

    [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        self.headerLabel.frame = CGRectMake(0, 0, 320, 20);
        self.headerView.frame = CGRectMake(0, 0, 320, 20);


    } completion:^(BOOL finished) {


    [UIView animateWithDuration:.5 delay:5.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        //UnHide the Status Bar
        statusBarHidden = FALSE;
        [self setNeedsStatusBarAppearanceUpdate];

        self.headerLabel.frame = CGRectMake(0, -20, 320, 20);
        self.headerView.frame = CGRectMake(0, -20, 320, 20);



    } completion:^(BOOL finished) {


        [self.headerView removeFromSuperview];
        [self.headerLabel removeFromSuperview];

    }];
}];

}

只有动画的后半部分才能正常工作,消息会正常滑回,状态栏会显示在其下方。但是,动画的前半部分,当视图向下滑动时,状态栏消失,导致我的导航栏向上移动,搞砸了我的动画。如何将导航栏保留在最初放置的位置,即使状态栏已消失

1 个答案:

答案 0 :(得分:2)

此代码以纵向方式工作,但需要进行修改才能在两个方向上正常工作。它使用单独的UIWindow作为叠加层,因此无需隐藏或取消隐藏状态栏。

@interface ViewController ()
@property (strong,nonatomic) UIWindow *dropdown;
@property (strong,nonatomic) UILabel *label;
@property (strong,nonatomic) UIWindow *win;
@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.dropdown = [[UIWindow alloc] initWithFrame:CGRectMake(0, -20, 320, 20)];
    self.dropdown.backgroundColor = [UIColor redColor];
    self.label = [[UILabel alloc] initWithFrame:self.dropdown.bounds];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:12];
    self.label.backgroundColor = [UIColor clearColor];
    [self.dropdown addSubview:self.label];
    self.dropdown.windowLevel = UIWindowLevelStatusBar;
    [self.dropdown makeKeyAndVisible];
    [self.dropdown resignKeyWindow];

}

- (IBAction)dropDown:(UIButton *)sender {
    [self animateHeaderViewWithText:@"This is my test string"];
}


-(void)animateHeaderViewWithText:(NSString *) text {
    self.label.text = text;
    CGRect frame = self.dropdown.frame;
    frame.origin.y = 0;
    [UIView animateWithDuration:.6 delay:0 options:0 animations:^{
        self.dropdown.frame = frame;
    }completion:^(BOOL finished) {
        ;
    }];
}