.h
#import <UIKit/UIKit.h>
#define kHeight [UIScreen mainScreen].bounds.size.height
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kShitOutScale 0.1f//缩放比例
#define kShitOutTop 5//上间距
#define kShitOutRight 5//侧间距
@interface LXViewController :UIViewController
typedefNS_ENUM(NSInteger, LXShowSide){//显示的界面
LXshowLeft=0,
LXshowCenter,
LXshowRight
};
@property(nonatomic,assign)LXShowSide currentSide;
//static CAKeyframeAnimation *kfAnimation(CGFloat p,UIView *view){
//
// CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
//
// return animation;
//}
typedefNS_ENUM(NSInteger, LXSGRside){
SGRLeft=0,
SGRRight,
SGRLeftAndRight
};
typedefNS_ENUM(NSInteger, LXAnimationType){
LXAnimationTypeNone=0,
LXAnimationTypeShitOut,
};
@property(nonatomic,assign)LXAnimationType currentAnimationType;
@property(nonatomic,strong)UISwipeGestureRecognizer *swipeLeft;//左手势
@property(nonatomic,strong)UISwipeGestureRecognizer *swipeRight;//右手势
@property(nonatomic,assign)BOOL isSGRAllExist;
@property(nonatomic,strong)CAKeyframeAnimation *kfAnimation;//帧动画
@property(nonatomic,assign)CGFloat distanceSeeLeft;//左边看到的中间视图的宽度
@property(nonatomic,assign)CGFloat distanceSeeRight;
@property(nonatomic,assign)CGFloat distanceTop;//距离上方的距离
@property(nonatomic,assign)CGFloat distanceBottom;
@property(nonatomic,strong)UIViewController *centerV;//中间的控制器
@property(nonatomic,strong)UIViewController *leftV;//左边的控制器
@property(nonatomic,strong)UIViewController *rightV;//右边的控制器
@property(nonatomic,assign)BOOL isShowShadow;//显示阴影
@property(nonatomic,assign)CGSize shadowOffset;
-(instancetype)initWithCenterVC:(UIViewController *)center leftVC:(UIViewController *)left rightVC:(UIViewController *)right;//创建
-(void)showLeft;
-(void)showCenter;
-(void)showRight;
-(void)addSGRinSide:(LXSGRside)SGRside;
-(void)removeSGRinSide:(LXSGRside)SGRside;
-(void)showCenterLeftDistance:(CGFloat)distanceSeeLeft andRightDistance:(CGFloat)distanceSeeRight;
-(void)showCenterTopDistance:(CGFloat)distanceTop andBottomDistance:(CGFloat)distanceBottom;
-(void)showCenterShadowConfigureWithOpacity:(CGFloat)opacity offset:(CGSize)size color:(CGColorRef)color;
@end
.m
#import "LXViewController.h"
@interface LXViewController ()
@end
@implementation LXViewController
CGFloatconst distanceSeeLeftDefault =50;
CGFloatconst distanceSeeRightDefault =50;
CGFloatconst distanceTopDefault =0;
CGFloatconst distanceBottomDefault =0;
#pragma mark-创建
-(instancetype)initWithCenterVC:(UIViewController *)center leftVC:(UIViewController *)left rightVC:(UIViewController *)right;{
if ([super init]) {
_centerV=center;
_leftV=left;
_rightV=right;
_isShowShadow=NO;//默认无阴影
_currentAnimationType=LXAnimationTypeNone;//默认侧滑动画,LXAnimationTypeNone
[self.viewaddSubview:_centerV.view];
self.currentSide=LXshowCenter;
[selfshowCenter];
[selfaddSGRinSide:SGRLeftAndRight];
_distanceSeeLeft=distanceSeeLeftDefault;
_distanceSeeRight=distanceSeeRightDefault;
_distanceTop=distanceTopDefault;
_distanceBottom=distanceBottomDefault;
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
}
#pragma mark-手势和触发动作
-(void)addLeftSwipe{
_swipeLeft=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeLeftAction)];
_swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.viewaddGestureRecognizer:_swipeLeft];
}
-(void)swipeLeftAction{
NSLog(@"向左清扫");
switch (self.currentSide) {
case LXshowLeft:
[selfshowCenter];
break;
caseLXshowCenter:
[selfshowRight];
break;
default:
break;
}
}
-(void)addRightSwipe{
_swipeRight=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeRightAction)];
_swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
[self.viewaddGestureRecognizer:_swipeRight];
}
-(void)swipeRightAction{
NSLog(@"向右清扫");
switch (self.currentSide) {
caseLXshowRight:
[selfshowCenter];
break;
caseLXshowCenter:
[selfshowLeft];
break;
default:
break;
}
}
#pragma mark-添加移除手势
-(void)addSGRinSide:(LXSGRside)SGRside{
switch (SGRside) {
case SGRLeft:
[selfaddLeftSwipe];
break;
case SGRRight:
[selfaddRightSwipe];
break;
caseSGRLeftAndRight:
[selfaddRightSwipe];
[selfaddLeftSwipe];
self.isSGRAllExist=YES;
break;
default:
break;
}
}
-(void)removeSGRinSide:(LXSGRside)SGRside{
switch (SGRside) {
case SGRLeft:
[self.viewremoveGestureRecognizer:_swipeLeft];
break;
case SGRRight:
[self.viewremoveGestureRecognizer:_swipeRight];
break;
caseSGRLeftAndRight:
[self.viewremoveGestureRecognizer:_swipeLeft];
[self.viewremoveGestureRecognizer:_swipeRight];
self.isSGRAllExist=NO;
break;
default:
break;
}
}
#pragma mark-显示中左右侧视图
-(void)showCenter;{
if (_currentAnimationType==LXAnimationTypeNone) {
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.frame=self.view.bounds;
}];
}
if (_currentAnimationType==LXAnimationTypeShitOut) {
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.transform=CGAffineTransformIdentity;
}];
}
if ([self.viewviewWithTag:1001]||[self.viewviewWithTag:1002]) {
[[self.viewviewWithTag:1001]removeFromSuperview];
[[self.viewviewWithTag:1002]removeFromSuperview];
}
if (_leftV) {
[_leftVremoveFromParentViewController];
}
if (_rightV) {
[_rightVremoveFromParentViewController];
}
self.currentSide=LXshowCenter;
}
-(void)showLeft;{
_leftV.view.frame=CGRectMake(0,0, kWidth,kHeight);
[self.viewinsertSubview:_leftV.viewbelowSubview:_centerV.view];
if (_currentAnimationType==LXAnimationTypeNone) {
_leftV.view.frame=CGRectMake(0,0, kWidth-_distanceSeeLeft,kHeight);
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.frame=CGRectMake(kWidth-_distanceSeeLeft,_distanceTop, kWidth,kHeight-_distanceBottom-_distanceTop);
_shadowOffset=CGSizeMake(-ABS(_shadowOffset.width),_shadowOffset.height);
self.centerV.view.layer.shadowOffset=_shadowOffset;
}];
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];//返回中心的按钮
button.frame=CGRectMake(kWidth-_distanceSeeLeft,_distanceTop, _distanceSeeLeft,kHeight-_distanceBottom-_distanceTop);
button.backgroundColor=[UIColorclearColor];
button.tag=1001;
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(showCenter)forControlEvents:UIControlEventTouchUpInside];
}
if (_currentAnimationType==LXAnimationTypeShitOut) {
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.transform=CGAffineTransformConcat(CGAffineTransformMakeScale(kShitOutScale,kShitOutScale), CGAffineTransformMakeTranslation(kWidth/2-kWidth/(1.0/kShitOutScale)/2-kShitOutRight, -(kHeight/2-kHeight/(1.0/kShitOutScale)/2-kShitOutTop)));
}];
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];//返回中心的按钮
button.frame=CGRectMake(kWidth-kShitOutRight-kWidth*kShitOutScale,kShitOutTop, kWidth*kShitOutScale,kHeight*kShitOutScale);
button.backgroundColor=[UIColorclearColor];
button.tag=1001;
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(showCenter)forControlEvents:UIControlEventTouchUpInside];
}
self.currentSide=LXshowLeft;
}
-(void)showRight;{
_rightV.view.frame=CGRectMake(0,0, kWidth,kHeight);
[self.viewinsertSubview:_rightV.viewbelowSubview:_centerV.view];
if (_currentAnimationType==LXAnimationTypeNone) {
_rightV.view.frame=CGRectMake(_distanceSeeRight,0, kWidth-_distanceSeeRight,kHeight);
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.frame=CGRectMake(_distanceSeeRight-kWidth,_distanceTop, kWidth,kHeight-_distanceBottom-_distanceTop);
_shadowOffset=CGSizeMake(ABS(_shadowOffset.width),_shadowOffset.height);
self.centerV.view.layer.shadowOffset=_shadowOffset;
}];
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];//返回中心的按钮
button.frame=CGRectMake(0,_distanceTop,_distanceSeeRight, kHeight-_distanceBottom-_distanceTop);
button.backgroundColor=[UIColorclearColor];
button.tag=1002;
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(showCenter)forControlEvents:UIControlEventTouchUpInside];
}
if (_currentAnimationType==LXAnimationTypeShitOut) {
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.transform=CGAffineTransformConcat(CGAffineTransformMakeScale(kShitOutScale,kShitOutScale), CGAffineTransformMakeTranslation(-(kWidth/2-kWidth/(1.0/kShitOutScale)/2-kShitOutRight), -(kHeight/2-kHeight/(1.0/kShitOutScale)/2-kShitOutTop)));
}];
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];//返回中心的按钮
button.frame=CGRectMake(kShitOutRight,kShitOutTop, kWidth*kShitOutScale,kHeight*kShitOutScale);
button.backgroundColor=[UIColorclearColor];
button.tag=1002;
[self.viewaddSubview:button];
[button addTarget:selfaction:@selector(showCenter)forControlEvents:UIControlEventTouchUpInside];
}
self.currentSide=LXshowRight;
}
#pragma mark-距离设置
-(void)showCenterLeftDistance:(CGFloat)distanceSeeLeft andRightDistance:(CGFloat)distanceSeeRight;{
_distanceSeeLeft=distanceSeeLeft;
_distanceSeeRight=distanceSeeRight;
}
-(void)showCenterTopDistance:(CGFloat)distanceTop andBottomDistance:(CGFloat)distanceBottom;{
_distanceTop=distanceTop;
_distanceBottom=distanceBottom;
}
#pragma mark-阴影的配置
-(void)showCenterShadowConfigureWithOpacity:(CGFloat)opacity offset:(CGSize)size color:(CGColorRef)color;{
if (self.isShowShadow==YES) {
self.centerV.view.layer.shadowOpacity=opacity;
self.shadowOffset=size;
self.centerV.view.layer.shadowColor=color;
}
}
#pragma mark-另类动画制作,代码中并未用到此处,用于增加LXAnimationType测试用的
-(BOOL)animatedRotationY{
_kfAnimation=[CAKeyframeAnimationanimationWithKeyPath:@"transform.rotation.y"];
CGFloat angle=M_PI;
_kfAnimation.values=@[@0,@(angle/3),@(angle*2/3),@(angle),@(angle*4/3)];
CAKeyframeAnimation *translation=[CAKeyframeAnimationanimationWithKeyPath:@"transform.translation.x"];
translation.values=@[@0,@(kWidth/8),@(kWidth/4),@(kWidth*3/8),@(kWidth/2)];
CAAnimationGroup *group=[CAAnimationGroupanimation];
group.animations=@[_kfAnimation,
translation
];
group.duration=1;
group.repeatCount=1;
//完成动画时,最终状态是否需要保留
group.removedOnCompletion =NO;
group.fillMode =kCAFillModeBoth;
[_centerV.view.layeraddAnimation:group forKey:nil];
// _centerV.view.layer.transform=CATransform3DMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>, <#CGFloat tz#>)
return YES;
}
-(void)shitOut{
[UIViewanimateWithDuration:.3animations:^{
_centerV.view.transform=CGAffineTransformConcat(CGAffineTransformMakeScale(.2,.2), CGAffineTransformMakeTranslation(kWidth/2-60, -kHeight/2+60));
}];
}
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIWindow *window=[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
self.window=window;
window.backgroundColor=[UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
MainNavViewController *mainC=[[UIStoryboardstoryboardWithName:@"MainStoryboard"bundle:nil]instantiateInitialViewController];
LeftViewController *leftC=[[UIStoryboardstoryboardWithName:@"LeftStoryboard"bundle:nil]instantiateInitialViewController];
RightNavViewController *rightC=[[UIStoryboardstoryboardWithName:@"RightStoryboard"bundle:nil]instantiateInitialViewController];
_lxC=[[LXViewControlleralloc]initWithCenterVC:mainCleftVC:leftC rightVC:rightC];
_lxC.isShowShadow=YES;
[_lxCshowCenterShadowConfigureWithOpacity:.3offset:CGSizeMake(5,5) color:[UIColorblackColor].CGColor];
[_lxCshowCenterLeftDistance:30andRightDistance:30];
self.window.rootViewController=_lxC;
return YES;
}
RightTableViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
AppDelegate *delegate=[UIApplicationsharedApplication].delegate;
[delegate.lxCshowCenterLeftDistance:0andRightDistance:0];//当选择的时候让侧滑消失
[delegate.lxCshowRight];
[delegate.lxCremoveSGRinSide:SGRLeftAndRight];
}
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:YES];
AppDelegate *delegate=[UIApplicationsharedApplication].delegate;
[delegate.lxCshowCenterLeftDistance:30andRightDistance:30];
[delegate.lxCshowRight];
}
-(void)viewDidAppear:(BOOL)animated{
[superviewDidAppear:YES];
AppDelegate *delegate=[UIApplicationsharedApplication].delegate;
if (delegate.lxC.isSGRAllExist==NO) {
[delegate.lxCaddSGRinSide:SGRLeftAndRight];
NSLog(@"走了");
}
}
@end