[ios]自定义简单的抽屉效果

        之前做一个项目,要实现抽屉的效果,当时在网上找控件找了挺久,确实有找到类似的。

类似下面:

 http://code4app.com/ios/%E5%B7%A6%E5%8F%B3%E6%BB%91%E5%8A%A8%E8%A7%86%E5%9B%BE/53717c40933bf0e0138b582d

http://code4app.com/ios/MMDrawerController/51b3fd056803fa152e000000

等等,还挺多的。


      不过我试用了,都觉得不是很满足客户的要求。(因为他们安卓端先做好,现在要求我ios抄它安卓的效果。)

      无办法,那就自己做一个吧。

      当时也是网上看到一个贴子得到灵感。这里忘记贴子的链接了(尴尬时间有点久)


     原理:

            在一个viewcontroller里面放两个view,一个左view,一个右view。通过左view显示列表,右view覆盖在上面。然后用手控制右view可以动态移动从而达到抽屉的效果。


    效果图如下:

    




为了方便贴码,我把图片都改成颜色块


//
//  LeftView.h
//  SlideView
//
//  Created by yuxuhong on 13-8-13.
//  Copyright (c) 2013年 yuxuhong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LeftView : UIView

@property (nonatomic, copy) void (^pressLeftViewList)(int tag);

@end


//
//  LeftView.m
//  SlideView
//
//  Created by yuxuhong on 13-8-13.
//  Copyright (c) 2013年 yuxuhong. All rights reserved.
//

#import "LeftView.h"

#define IS_IPHONE_VERSION(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@implementation LeftView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initLayout];
    }
    return self;
}

-(void)initLayout{
    float yy=0;
    if (IS_IPHONE_VERSION(@"7.0"))
        yy=20;
    
    //左边背景
    [self setBackgroundColor:[UIColor whiteColor]];
    
    //左边的top
    UIButton *leftTop = [[UIButton alloc]initWithFrame:CGRectMake(0, yy, 180, 44)];
    [leftTop setTitle:@"top" forState:UIControlStateNormal];
    [leftTop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftTop setBackgroundColor:[UIColor greenColor]];
    [self addSubview:leftTop];
    
    
    //tag1
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 40+yy, 300, 44)];
    [button addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag=1;
    [button setTitle:@"Tag 1" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets=UIEdgeInsetsMake(0, 50, 0, 0);
    [self addSubview:button];
    
    UIView *aline=[[UIView alloc]initWithFrame:CGRectMake(0, 40+yy, 300, 1)];
    aline.backgroundColor = [UIColor whiteColor];
    [self addSubview:aline];
    
    UIView *aIcon=[[UIView alloc]initWithFrame:CGRectMake(18, 52+yy, 20, 20)];
    aIcon.backgroundColor = [UIColor redColor];
    [self addSubview:aIcon];
 
    
    
    //tag 2
    button = [[UIButton alloc]initWithFrame:CGRectMake(0, 84+yy, 300, 44)];
    [button addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag=2;
    [button setTitle:@"Tag 2" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets=UIEdgeInsetsMake(0, 50, 0, 0);
    [self addSubview:button];
    
    aline=[[UIView alloc]initWithFrame:CGRectMake(0, 83+yy, 300, 1)];
    aline.backgroundColor = [UIColor whiteColor];
    [self addSubview:aline];
    
    aIcon=[[UIView alloc]initWithFrame:CGRectMake(18, 96+yy, 20, 20)];
    aIcon.backgroundColor = [UIColor redColor];
    [self addSubview:aIcon];
 
    
}

-(void)pressAction:(UIButton *)sender{
    self.pressLeftViewList((int)sender.tag);
}

@end

//
//  CenterView.h
//  SlideView
//
//  Created by yuxuhong on 13-8-13.
//  Copyright (c) 2013年 yuxuhong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CenterView : UIView
{
    UIPanGestureRecognizer *panGestureRecognizer;
    float centerX;
    float centerY;
    
    UIButton *closeLeft;
}

@property (nonatomic, copy) void (^flesh)();



@end


//
//  CenterView.m
//  SlideView
//
//  Created by yuxuhong on 13-8-13.
//  Copyright (c) 2013年 yuxuhong. All rights reserved.
//

#import "CenterView.h"

#define MAX_CENTER_X 340
#define BOUND_X 250

#define IS_IPHONE_VERSION(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)


@implementation CenterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognizer];
        
        [self initLayout];
    }
    return self;
}

-(void)initLayout{
    
    CGRect screen = [[UIScreen mainScreen] bounds];
    centerX = screen.size.width / 2;
    centerY = screen.size.height / 2;
    

    CGRect frame = [[UIScreen mainScreen] bounds];
    float yy=0;
    if (IS_IPHONE_VERSION(@"7.0"))
        yy=20;
    
    UIView *bg=[[UIView alloc]initWithFrame:CGRectMake(0, yy, frame.size.width, frame.size.height)];
    [bg setBackgroundColor:[UIColor lightGrayColor]];
    [self addSubview:bg];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, yy+2, 55, 40)];
    [button setBackgroundColor:[UIColor blueColor]];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 44)];
    label.text=@"Home";
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=UITextAlignmentCenter;
    [self addSubview:label];
    
    button = [[UIButton alloc]initWithFrame:CGRectMake(270, yy+2, 55, 40)];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"刷新" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(fleshButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];

    
    
    closeLeft =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 540)];
    closeLeft.backgroundColor=[UIColor clearColor];
    [closeLeft addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    closeLeft.hidden=YES;
    [self addSubview:closeLeft];
}

-(void)showHalf{
    self.center = CGPointMake(MAX_CENTER_X, centerY);
    closeLeft.hidden=NO;
}
-(void)showFull{
    self.center = CGPointMake(centerX, centerY);
    closeLeft.hidden=YES;

    self.flesh();
}
- (void)buttonPressed:(UIButton *)button
{
    [UIView animateWithDuration:0.2 animations:^(void){
        
        if (self.center.x == centerX) {
            [self showHalf];
        }else if (self.center.x == MAX_CENTER_X){
            [self showFull];
        }
        
    }];
    
}

-(void) handlePan:(UIPanGestureRecognizer*)recognizer
{
    CGPoint translation = [recognizer translationInView:self];
    float x = self.center.x + translation.x;
    if (x < centerX) {
        x = centerX;
    }
    if (x>MAX_CENTER_X) {
        x=MAX_CENTER_X;
    }
    self.center = CGPointMake(x, centerY);
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        [UIView animateWithDuration:0.2 animations:^(void){
            
            if (x > BOUND_X) {
                [self showHalf];
            }else{
                [self showFull];
            }
            
        }];
        
        
    }
    
    [recognizer setTranslation:CGPointZero inView:self];
}


-(void)fleshButton:(UIButton *)sender{
    self.flesh();
}
@end


用法是在一个uiviewcontroller里使用,参与如下:

//
//  ViewController.m
//  SideBarController
//
//  Created by youmi on 14-8-8.
//  Copyright (c) 2014年 youmi. All rights reserved.
//

#import "ViewController.h"
#import "LeftView.h"
#import "CenterView.h"
#import "SecondViewController.h"

@interface ViewController ()
@property (nonatomic, strong) LeftView *leftView;
@property (nonatomic, strong) CenterView *centerView;
@end

@implementation ViewController
@synthesize leftView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CGRect screen = [[UIScreen mainScreen] bounds];
    CGFloat width = screen.size.width;
    CGFloat height = screen.size.height;
    
    ViewController *_self=self;
    
    leftView = [[LeftView alloc] init];
    leftView.frame = CGRectMake(0, 0, width, height);
    leftView.pressLeftViewList=^(int tag){
        if (tag==1) {
            SecondViewController *controller =[[SecondViewController alloc]init];
            [_self presentViewController:controller animated:YES completion:nil];
        }
        else if (tag==2) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"test" message:@"tag 2" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
        }
        
    };
    [self.view addSubview:leftView];
    
    self.centerView = [[CenterView alloc] init];
    self.centerView.frame = CGRectMake(0, 0, width, height);
    self.centerView.flesh=^{
        NSLog(@"刷新主界面");
    };
    [self.view addSubview:self.centerView];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值