四种常用风格的抽屉视图

1)双向抽屉视图

2)抽屉动画

3)双向抽屉视图带缩放效果

4)抽屉视图动画缩放

#import "ApDrawerController.h"#import "AppDelegate.h"#import "leftVC.h"#import "rightVC.h"@interface ApDrawerController (){

CGFloat _scalef;  //实时横向位移

}

@property (nonatomic,strong) UITableView *leftTableview;

@property (nonatomic,assign) CGFloat leftTableviewW;

@property (nonatomic,strong) UIView *contentView;

@property (nonatomic,strong) UITableView *rightTableview;

@end

@implementation ApDrawerController

- (void)viewDidLoad {

[super viewDidLoad];

//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(judge) name:UIWindowDidBecomeHiddenNotification object:nil];

}

//-(void)judge{

//    self.rightVC.view.hidden=YES;

//    self.leftVC.view.hidden=NO;

//}

/**

@brief 初始化侧滑控制器

@param leftVC 左视图控制器

@param rightVC 右视图控制器

centerVC 中间视图控制器

@result instancetype 初始化生成的对象

*/

- (instancetype)initWithCenterViewController:(UIViewController *)centerVC leftViewController:(UIViewController *)leftVC RightViewController:(UIViewController *)rightVC{

self = [super init];

if(self){

self.speedf = vSpeedFloat;

self.leftVC = leftVC;

self.centerVC = centerVC;

self.rightVC=rightVC;

//滑动手势

self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

[self.centerVC.view addGestureRecognizer:self.pan];

[self.pan setCancelsTouchesInView:YES];

self.pan.delegate = self;

self.leftVC.view.hidden = YES;

self.rightVC.view.hidden=YES;

[self addChildViewController:leftVC];

[self.view addSubview:self.leftVC.view];

[self addChildViewController:rightVC];//确定界面切换方式

[self.view addSubview:self.rightVC.view];//界面可视区展现

//蒙版

UIView* viewl = [[UIView alloc] init];

viewl.frame = self.leftVC.view.bounds;

viewl.backgroundColor = [UIColor blackColor];

viewl.alpha = 0.5;

self.contentView = viewl;

[self.leftVC.view addSubview:viewl];

//蒙版

UIView *viewr = [[UIView alloc] init];

viewr.frame = self.rightVC.view.bounds;

viewr.backgroundColor = [UIColor blackColor];

viewr.alpha = 0.5;

self.contentView = viewr;

[self.leftVC.view addSubview:viewr];

//获取左侧tableview????????

for (UIView *obj in self.leftVC.view.subviews) {

if ([obj isKindOfClass:[UITableView class]]) {

self.leftTableview = (UITableView *)obj;

}

}

//获取右侧tableview????????

for (UIView *obj in self.rightVC.view.subviews) {

if ([obj isKindOfClass:[UITableView class]]) {

self.rightTableview = (UITableView *)obj;

}

}

self.leftTableview.backgroundColor = [UIColor clearColor];

self.leftTableview.frame = CGRectMake(0, 0, kScreenWidth - kMainPageDistance, kScreenHeight);

//设置左侧tableview的初始位置和缩放系数

self.leftTableview.transform = CGAffineTransformMakeScale(kLeftScale, kLeftScale);

self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

self.rightTableview.backgroundColor = [UIColor clearColor];

self.rightTableview.frame = CGRectMake(0, 0, kScreenWidth - kMainPageDistance, kScreenHeight);

//设置右侧tableview的初始位置和缩放系数

self.rightTableview.transform = CGAffineTransformMakeScale(kLeftScale, kLeftScale);

self.rightTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

//        [self.view addSubview:self.centerVC.view];

[self addChildViewController:self.centerVC];

[self.view addSubview:self.centerVC.view];

self.closed = YES;//初始时侧滑窗关闭

}

return self;

}

- (void) viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

self.leftVC.view.hidden = NO;

}

#pragma mark - 滑动手势

//滑动手势

- (void) handlePan: (UIPanGestureRecognizer *)rec{

CGPoint point = [rec translationInView:self.view];

_scalef = (point.x * self.speedf + _scalef);

BOOL needMoveWithTap = YES;  //是否还需要跟随手指移动

if (((self.centerVC.view.frame.origin.x <= 0) && (_scalef <= 0)) || ((self.centerVC.view.frame.origin.x >= (kScreenWidth - kMainPageDistance )) && (_scalef >= 0)))

{

//边界值管控

_scalef = 0;

needMoveWithTap = NO;

}

//根据视图位置判断是左滑还是右边滑动

if (needMoveWithTap && (rec.view.frame.origin.x >= 0) && (rec.view.frame.origin.x <= (kScreenWidth - kMainPageDistance)))

{

CGFloat recCenterX = rec.view.center.x + point.x * self.speedf;

if (recCenterX < kScreenWidth * 0.5 - 2) {

recCenterX = kScreenWidth * 0.5;

}

CGFloat recCenterY = rec.view.center.y;

rec.view.center = CGPointMake(recCenterX,recCenterY);

//scale 1.0~kMainPageScale

CGFloat scale = 1 - (1 - kLeftMainPageScale) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale, scale);

[rec setTranslation:CGPointMake(0, 0) inView:self.view];

CGFloat leftTabCenterX = kLeftCenterX + ((kScreenWidth - kMainPageDistance) * 0.5 - kLeftCenterX) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

//        NSLog(@"%f",leftTabCenterX);

//leftScale kLeftScale~1.0

CGFloat leftScale = kLeftScale + (1 - kLeftScale) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

self.leftTableview.center = CGPointMake(leftTabCenterX, kScreenHeight * 0.5);

self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity, leftScale,leftScale);

//tempAlpha kLeftAlpha~0

CGFloat tempAlpha = kLeftAlpha - kLeftAlpha * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

self.contentView.alpha = tempAlpha;

}

else

{//**********************************手势范围************************************************

//超出范围,

if (self.centerVC.view.frame.origin.x < 0)

{

[self closeLeftView];

_scalef = 0;

}else if (self.centerVC.view.frame.origin.x >0&&self.centerVC.view.frame.origin.x<(kScreenWidth - kMainPageDistance)){

//            self.rightVC.view.hidden=YES;

//            self.leftVC.view.hidden=NO;

//

}else if (self.centerVC.view.frame.origin.x > (kScreenWidth - kMainPageDistance)){

[self openLeftView];//self.centerVC.view仿射变换

_scalef = 0;

}

}

//手势结束后修正位置,超过约一半时向多出的一半偏移

if (rec.state == UIGestureRecognizerStateEnded) {

if (fabs(_scalef) > vCouldChangeDeckStateDistance)

{

if (self.closed)

{

[self openLeftView];

}

else

{

[self closeLeftView];

}

}

else

{

if (self.closed)

{

[self closeLeftView];

}

else

{

[self openLeftView];

}

}

_scalef = 0;

}

}

#pragma mark - 单击手势

-(void)handeTap:(UITapGestureRecognizer *)tap{

if ((!self.closed) && (tap.state == UIGestureRecognizerStateEnded))

{

[UIView beginAnimations:nil context:nil];

tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);

self.closed = YES;

self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

self.contentView.alpha = kLeftAlpha;

[UIView commitAnimations];

_scalef = 0;

[self removeSingleTap];

}

}

#pragma mark - 修改视图位置

/**

@brief 关闭左视图

*/

- (void)closeLeftView

{

[UIView beginAnimations:nil context:nil];

self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

self.centerVC.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);

self.closed = YES;

self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

self.contentView.alpha = kLeftAlpha;

[UIView commitAnimations];

[self removeSingleTap];

}

/**

@brief 关闭右视图

*/

- (void)closeRightView

{

[UIView beginAnimations:nil context:nil];

self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

self.centerVC.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);

self.closed = YES;

self.rightTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

self.rightTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

self.contentView.alpha = kLeftAlpha;

[UIView commitAnimations];

[self removeSingleTap];

}

/**

@brief 打开左视图

*/

- (void)openLeftView

{

[UIView beginAnimations:nil context:nil];

//*************************界面展现布局效果****************************************************

self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftMainPageScale,kLeftMainPageScale);

self.centerVC.view.center = kLeftBarButtonItemMainPageCenter;

//*************************界面展现布局效果****************************************************

self.closed = NO;

self.leftTableview.center = CGPointMake((kScreenWidth - kMainPageDistance) * 0.5, kScreenHeight * 0.5);

self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

self.contentView.alpha = 0;

self.rightVC.view.hidden=YES;

self.leftVC.view.hidden=NO;

[(leftVC*)self.leftVC setCATransitionWithMainType:CameraIrisHollowOpen SubType:CATransitionFromRight];

[UIView commitAnimations];

[self disableTapButton];

}

/**

@brief 打开右视图

*/

- (void)openRightView

{

[UIView beginAnimations:nil context:nil];

//*************************界面展现布局效果****************************************************

self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,kRightMainPageScale,kLeftMainPageScale);

self.centerVC.view.center =kRightBarButtonItemMainPageCenter;

//*************************界面展现布局效果****************************************************

self.closed = NO;

self.rightTableview.center = CGPointMake((kScreenWidth - kMainPageDistance) * 0.5, kScreenHeight * 0.5);

self.rightTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

self.contentView.alpha = 0;

self.rightVC.view.hidden=NO;

self.leftVC.view.hidden=YES;

[(rightVC*)self.rightVC setCATransitionWithMainType:OglFlip SubType:CATransitionFromRight];

[UIView commitAnimations];

[self disableTapButton];

}

#pragma mark - 行为收敛控制

- (void)disableTapButton

{

for (UIButton *tempButton in [_centerVC.view subviews])

{

[tempButton setUserInteractionEnabled:NO];

}

//单击

if (!self.sideslipTapGes)

{

//单击手势

self.sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];

[self.sideslipTapGes setNumberOfTapsRequired:1];

[self.centerVC.view addGestureRecognizer:self.sideslipTapGes];

self.sideslipTapGes.cancelsTouchesInView = YES;  //点击事件盖住其它响应事件,但盖不住Button;

}

}

//关闭行为收敛

- (void) removeSingleTap

{

for (UIButton *tempButton in [self.centerVC.view  subviews])

{

[tempButton setUserInteractionEnabled:YES];

}

[self.centerVC.view removeGestureRecognizer:self.sideslipTapGes];

self.sideslipTapGes = nil;

}

/**

*  设置滑动开关是否开启

*

*  @param enabled YES:支持滑动手势,NO:不支持滑动手势

*/

- (void)setPanEnabled: (BOOL) enabled

{

[self.pan setEnabled:enabled];

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

return YES;

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {

if(self.beginSlide != nil)

{

self.beginSlide();

}

if(touch.view.tag == vDeckCanNotPanViewTag)

{

//        NSLog(@"不响应侧滑");

return NO;

}

else

{

//        NSLog(@"响应侧滑");

return YES;

}

}

@end

@implementation UIViewController (ApDrawerController)

- (ApDrawerController *)apDrawerCtrl

{

if([self.parentViewController isKindOfClass:[ApDrawerController class]]){

return (ApDrawerController*)self.parentViewController;

}

else if([self.parentViewController isKindOfClass:[UINavigationController class]] &&

[self.parentViewController.parentViewController isKindOfClass:[ApDrawerController class]]){

return (ApDrawerController *)self.parentViewController.parentViewController;

}

else{

return nil;

}

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容