双击UIview时隐藏导航栏

时间:2012-01-18 11:18:16

标签: iphone ios storyboard

如何在不使用导航控制器的情况下双击隐藏导航栏并在UIView上单击显示?还有隐藏和展示时的动画?

提前致谢。

3 个答案:

答案 0 :(得分:0)

尝试隐藏

[UIView beginAnimations:@"Hide bar animation" context:NULL];

[UIView setAnimationDuration:0.5];

navigationBar.alpha = 0.0;

[UIView commitAnimations];

for showing

[UIView beginAnimations:@"Show bar animation" context:NULL];

[UIView setAnimationDuration:0.5];

navigationBar.alpha = 1.0;

[UIView commitAnimations];

但是使用UINavigationController太容易了......

答案 1 :(得分:0)

UITapGestureRecognizer* tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(your_selector)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

在你的选择器中:

-(void)your_selector{
    [UIView animateWithDuration:0.5
                 animations:^{
                     bar.frame = CGRectOffset(bar.frame, 0, bar.bounds.size.height*bar.frame.origin.y<0?-1:1);
                 }];
}

在此实现中,您只有双触事件,它会将bar的状态更改为反向。 不幸的是,单一和双触摸手势在某一时刻并没有简单的实现。更多的人类行为,对你而言也很容易 - 只有双重触摸才能工作。

XCode project - example

答案 2 :(得分:0)

您可以拥有一个没有导航控制器的UINavigationBar

navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
[navBar setDelegate:self];
[mainView addSubview:navBar];

UITapGestureRecognizerUIGestureRecognizer的具体子类,用于查找单个或多个点击:

UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideNavBar:)];
[doubleTap setNumberOfTapsRequired:2];
[YOURVIEW addGestureRecognizer:doubleTap];

UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNavBar:)];
[singleTap setNumberOfTapsRequired:1];
[YOURVIEW addGestureRecognizer:singleTap];

以下是显示或隐藏导航栏的方法:

- (void)hideNavBar:(id)sender 
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f]; //Animation duration in seconds

    navBar.alpha = 0.0f;

    [UIView commitAnimations];
}

- (void)showNavBar:(id)sender 
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f]; //Animation duration in seconds

    navBar.alpha = 1.0f;

    [UIView commitAnimations];
}