如題,關於抽屜式導航,CocoaChina上也有篇總結,有興趣的也可以去了解,http://www.cocoachina.com/newbie/tutorial/2012/0720/4475.html
第一份demo:在一個ViewController中定義了各種frame不同的UIView,然后在點擊或者手勢滑動的時候現實隱藏相應的view就是了。 十分簡陋,不過效果還是有一點的。因為簡陋,代碼就不貼出來了,說了思路就行。
第二份demo:其實思路都是一樣的,改變相應的view的frame,然后讓其顯示。參考了cocoa上的一份代碼(http://www.cocoachina.com/iphonedev/toolthain/2011/1222/3768.html)
里面有把彈出的導航放在不同的ViewController中,然后在appDelegate中讓其現實不同frame的view。
效果如圖:
其中關鍵的代碼:
- (void)makeLeftViewVisible { self.root.view.layer.shadowColor = [UIColor blueColor].CGColor; self.root.view.layer.shadowOpacity = 0.4f; self.root.view.layer.shadowOffset = CGSizeMake(-12.0, 1.0f); self.root.view.layer.shadowRadius = 7.0f; self.root.view.layer.masksToBounds = NO; [self moveToRightSide]; [self.leftViewController setVisible:YES]; } // move view to right side - (void)moveToRightSide { [self animateHomeViewToSide:CGRectMake(270.0f, self.root.view.frame.origin.y, self.root.view.frame.size.width, self.root.view.frame.size.height)]; } // animate home view to side rect - (void)animateHomeViewToSide:(CGRect)newViewRect { [UIView animateWithDuration:0.2 animations:^{ self.root.view.frame = newViewRect; } completion:^(BOOL finished){ UIControl *overView = [[UIControl alloc] init]; overView.tag = 10086; overView.backgroundColor = [UIColor clearColor]; overView.frame = self.root.view.frame; [overView addTarget:self action:@selector(restoreViewLocation) forControlEvents:UIControlEventTouchDown]; [[[UIApplication sharedApplication] keyWindow] addSubview:overView]; }]; } //restore view - (void)restoreViewLocation { [UIView animateWithDuration:0.3 animations:^{ self.root.view.frame = CGRectMake(0, self.root.view.frame.origin.y, self.root.view.frame.size.width, self.root.view.frame.size.height); } completion:^(BOOL finished){ UIControl *overView = (UIControl *)[[[UIApplication sharedApplication] keyWindow] viewWithTag:10086]; [overView removeFromSuperview]; }]; }
我做的點修改,把上面代碼放在了appDelegate中,這樣就能在任何地方都調用實現彈出導航欄了。
另外我想在點擊左側的“aaa”“bbb”“ccc”時,使起切換到不同的controller,改變appdelegate中的self.window.rootViewController 就行。
一開始切換時不注意,使navi的bar沒顯示出來。最后發現是設置self.window.rootViewController直接設置了xxViewConroller,而沒有設置navi..Controller,
應該是下面的做法
- (void) setRootVC:(UIViewController *)root{ self.navi = [[UINavigationController alloc] initWithRootViewController:root]; [self.window setRootViewController:self.navi]; self.window.rootViewController.view.frame = self.root.view.frame; self.root = [self.window rootViewController]; NSLog(@"root.view.frame.x %f %f ",self.root.view.frame.origin.x,self.root.view.frame.origin.y); [self.window makeKeyAndVisible]; }