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