【问题标题】:Animating status bar iOS 7动画状态栏 iOS 7
【发布时间】:2013-12-17 06:16:28
【问题描述】:

我正在尝试(正在努力)在 iOS7 中为我的应用程序实现类似 snapchat 的错误消息显示(snapchat 错误消息以某种方式滑过状态栏)。我一辈子都无法让我的动画正常工作。这是我制作动画的方法

- (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];

    }];
}];

}

只有动画的后半部分真正正常工作,消息滑回正常,状态栏出现在其下方。然而,动画的前半部分,当视图向下滑动时,状态栏消失导致我的导航栏向上移动,搞砸了我的动画。如何让我的导航栏保持在最初放置的位置,即使状态栏消失了

【问题讨论】:

  • 调用setNeedsStatusBarAppearanceUpdate 方法会导致重绘视图并将导航栏移到顶部。在不这样做的情况下找到解决方法。
  • 你需要这个同时在纵向和横向上工作吗?
  • 只有肖像@rdelmar

标签: ios objective-c xcode animation statusbar


【解决方案1】:

此代码在纵向上工作,但需要修改才能在两个方向上正常工作。它使用单独的 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) {
        ;
    }];
}

【讨论】:

  • 这可行,但是在动画运行时用户不能使用导航栏。有什么办法我只能使用状态栏的快照,而不是整个导航栏,或者只是导航栏的一部分? @rdelmar
  • 实际上他们在动画运行时不能做任何事情,因为快照占据了整个屏幕。要是我知道 snapchat 是怎么做到的就好了。这似乎是不可能的哈哈
  • @Stonep123,你真的需要他们在那段时间做任何事情吗?您希望动画运行多长时间?
  • @Stonep123,我已将答案更改为使用 UIWindow 作为覆盖,这应该允许用户仍然与视图进行交互。
猜你喜欢
  • 2015-01-22
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 2013-08-14
  • 2015-06-04
相关资源
最近更新 更多