iOS左滑菜单类似抽屉的实现

这里写图片描述

1,有点击抽屉按钮的主界面的实现

//.m文件
#import "RightViewController.h"
#import "CustomRightView.h"

#define OPENCENTERX 220.0
#define DIVIDWIDTH 70.0 //OPENCENTERX 对应确认是否打开或关闭的分界线。
@interface RightViewController (){
    CustomRightView *customView;
}
@end

@implementation RightViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"抽屉" style:0 target:self action:@selector(btnClick)];
    self.navigationItem.rightBarButtonItem = btn;

    CGRect rect = CGRectMake(0, 0,
                            self.view.frame.size.width,
self.view.frame.size.height);
    NSLog(@"w:%f, h:%f", rect.size.width, rect.size.height);

    UIView *rightView = [[UIView alloc]init];
    rightView.frame = rect;
    rightView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:rightView];

   UIView *contentView = [[UIView alloc] initWithFrame:rect];
   contentView.backgroundColor = [UIColor blueColor];

   customView = [[CustomRightView alloc] initWithView:contentView parentView:self.view];
    [[customView layer] setShadowOffset:CGSizeMake(10, 10)];
    [[customView layer] setShadowRadius:20];
    [[customView layer] setShadowOpacity:1];
    [[customView layer] setShadowColor:[UIColor blackColor].CGColor];
    [self.view addSubview:customView];
}

- (void)btnClick
{
    [UIView animateWithDuration:0.5
                          delay:0.01
options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void){         
        customView.center = CGPointMake(-DIVIDWIDTH, self.view.frame.size.height/2);
        }completion:^(BOOL isFinish){
     }];
}

2,自定义右侧滑动视图,继承自UIView

//.h
#import <UIKit/UIKit.h>
@interface CustomRightView : UIView
{
    CGPoint openPointCenter;
    CGPoint closePointCenter;
}
-(id)initWithView:(UIView*)contentview parentView:(UIView*) parentview;
@property (nonatomic, strong) UIView *parentView; //抽屉视图的父视图
@end
//.m
#import "CustomRightView.h"
#define OPENCENTERX 220.0
#define DIVIDWIDTH 70.0 //OPENCENTERX 对应确认是否打开或关闭的分界线。
@implementation CustomRightView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithView:(UIView *)contentview parentView:(UIView *)parentview
{
    self = [super initWithFrame:CGRectMake(0,0,contentview.frame.size.width, contentview.frame.size.height)];
    if (self) 
    {
        self.parentView = parentview;
        [self addSubview:contentview];
        //添加拖动出现右侧菜单的手势
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self
action:@selector(handlePan:)];
        [self addGestureRecognizer:panGestureRecognizer];
        //添加点击返回关闭菜单的手势
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(handleTap:)];
        [self addGestureRecognizer:tapGestureRecognizer];

        openPointCenter = CGPointMake(self.parentView.center.x + OPENCENTERX,
self.parentView.center.y);
        NSLog(@"openPointCenter x:%f, openPointCenter y:%f",openPointCenter.x,openPointCenter.y);
        }
        return self;
}

-(void)handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint translation = [recognizer translationInView:self.parentView];
    float x = self.center.x + translation.x;
    NSLog(@"translation x:%f", translation.x);

    if (x > self.parentView.center.x) {
        x = self.parentView.center.x;
    }
    self.center = CGPointMake(x, openPointCenter.y);

    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5
                              delay:0.01
options:UIViewAnimationOptionCurveEaseInOut
                         animations:^(void){
             if (x <self.width- (openPointCenter.x -  DIVIDWIDTH)) {
             self.center = CGPointMake(-DIVIDWIDTH, openPointCenter.y);
             }
             else{
                 self.center = CGPointMake(openPointCenter.x - OPENCENTERX,
openPointCenter.y);
             }
             }completion:^(BOOL isFinish){
         }];
    }
    [recognizer setTranslation:CGPointZero inView:self.parentView];
}
-(void) handleTap:(UITapGestureRecognizer*) recognizer
{
    [UIView animateWithDuration:0.75
                          delay:0.01
options:UIViewAnimationOptionTransitionCurlUp animations:^(void){
        self.center = CGPointMake(openPointCenter.x - OPENCENTERX,openPointCenter.y);
        }
        completion:nil];
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值