Animation iOS7中带隐藏/显示选项卡栏动画的自动布局
我试图实现一个显示/隐藏标签栏的效果,内容将被扩展以填充标签栏曾经所在的空间 我找到了显示/隐藏选项卡栏的代码,对此我很满意(来源:) 我添加了以下代码以相应地定位按钮:Animation iOS7中带隐藏/显示选项卡栏动画的自动布局,animation,ios7,autolayout,show-hide,uitabbar,Animation,Ios7,Autolayout,Show Hide,Uitabbar,我试图实现一个显示/隐藏标签栏的效果,内容将被扩展以填充标签栏曾经所在的空间 我找到了显示/隐藏选项卡栏的代码,对此我很满意(来源:) 我添加了以下代码以相应地定位按钮: if (hiddenTabBar) { self.constraintToBottom.constant=0; [self.TestButton setNeedsUpdateConstraints]; } else { self.constraintToBottom.constant=-49;
if (hiddenTabBar) {
self.constraintToBottom.constant=0;
[self.TestButton setNeedsUpdateConstraints];
} else {
self.constraintToBottom.constant=-49;
[self.TestButton setNeedsUpdateConstraints];
}
[self.TestButton layoutIfNeeded];
它的工作原理与预期一致。除了按钮的动画。这是动画之前应用程序的初始屏幕:
这是在动画之后
我可以成功隐藏选项卡栏并用正确的动画定位按钮。但是,当我想再次显示选项卡栏时,按钮似乎从屏幕的底部(外部)开始,而不是第二张图中所示的位置。我已调整动画时间,以便在制作动画时捕捉屏幕:
下面是隐藏/显示选项卡栏操作的完整代码
- (IBAction)TestTapped:(id)sender {
[UIView beginAnimations:nil context:NULL];
if(hiddenTabBar)
[UIView setAnimationDuration:60];
else
[UIView setAnimationDuration:0.5];
for(UIView *view in self.tabBarController.view.subviews)
{
CGRect _rect = view.frame;
if([view isKindOfClass:[UITabBar class]])
{
if (hiddenTabBar) {
_rect.origin.y = 431;
[view setFrame:_rect];
} else {
_rect.origin.y = 480;
[view setFrame:_rect];
}
} else if(view==self.TestButton)
{
NSLog(@"ZZ");
}
else{
if (hiddenTabBar) {
_rect.size.height = 431;
[view setFrame:_rect];
} else {
_rect.size.height = 480;
[view setFrame:_rect];
}
}
}
if (hiddenTabBar) {
self.constraintToBottom.constant=0;
[self.TestButton setNeedsUpdateConstraints];
} else {
self.constraintToBottom.constant=-49;
[self.TestButton setNeedsUpdateConstraints];
}
[self.TestButton layoutIfNeeded];
[UIView commitAnimations];
hiddenTabBar =!hiddenTabBar;
}
我希望按钮的动画从第二个图表的确切位置开始。您可以使用此类别隐藏/显示带有动画的选项卡栏 ----------------------.h文件---------------------
#define screenSize ([[UIScreen mainScreen ] bounds])
@interface UITabBarController (HideTabBar)
- (void)hideTabBarAnimated:(BOOL)animated;
- (void)showTabBarAnimated:(BOOL)animated;
- (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition;
@end
#define kAnimationDuration .2
@implementation UITabBarController (HideTabBar)
- (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition
{
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
if (complition) {
complition();
}
}];
}
else
{
[self.view setFrame:tabBarControllerFrame];
if (complition) {
complition();
}
}
}
- (void)hideTabBarAnimated:(BOOL)animated
{
[self hideTabBarAnimated:animated complition:nil];
}
- (void)showTabBarAnimated:(BOOL)animated {
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
}];
}
else
[self.view setFrame:tabBarControllerFrame];
}
@end
----------------------.m文件---------------------
#define screenSize ([[UIScreen mainScreen ] bounds])
@interface UITabBarController (HideTabBar)
- (void)hideTabBarAnimated:(BOOL)animated;
- (void)showTabBarAnimated:(BOOL)animated;
- (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition;
@end
#define kAnimationDuration .2
@implementation UITabBarController (HideTabBar)
- (void)hideTabBarAnimated:(BOOL)animated complition:(void(^)())complition
{
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
if (complition) {
complition();
}
}];
}
else
{
[self.view setFrame:tabBarControllerFrame];
if (complition) {
complition();
}
}
}
- (void)hideTabBarAnimated:(BOOL)animated
{
[self hideTabBarAnimated:animated complition:nil];
}
- (void)showTabBarAnimated:(BOOL)animated {
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
}];
}
else
[self.view setFrame:tabBarControllerFrame];
}
@end
与viewcontroller一起使用,如
[self.tabBarController hideTabBarAnimated:YES];//You can use completion handler if you want.
也许这会解决你的问题