iphone:类似path的抽屉式导航效果的demo总结

如题,关于抽屉式导航,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];
    
}

 

 

posted on 2012-07-25 11:42  老Zhan  阅读(5935)  评论(1编辑  收藏  举报