简单的侧滑

#import "RootViewController.h"  //创建根视图

#import "MTabBarController.h" //右视图

#import "TableViewController.h" //左视图

#define SreenH  [UIScreen mainScreen].bounds.size.height  //屏幕的高

#define SreenW  [UIScreen mainScreen].bounds.size.width //屏幕的宽

@interface RootViewController ()

@property(strong,nonatomic)TableViewController *tbVc;

@property(strong,nonatomic)MTabBarController *mTab;

@property(strong,nonatomic)UIViewController *curretVC;

@property(nonatomic,strong)UIPanGestureRecognizer *pan2;//侧滑后的平移手势

@property(nonatomic,strong)UITapGestureRecognizer *tap;//轻拍手势

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    UIView *vw=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SreenW, 200)];

    vw.backgroundColor=[UIColor greenColor];

    [self.view    addSubview:vw];


    self.view.backgroundColor=[UIColor whiteColor];

    self.tbVc=[[TableViewController alloc]init];

    self.mTab=[[MTabBarController alloc]init];

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

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

    [self addChildViewController:self.tbVc];

    [self addChildViewController:self.mTab];

    [self.mTab didMoveToParentViewController:self];

    [self.tbVc didMoveToParentViewController:self];

    self.curretVC=self.tbVc;

    self.tbVc.view.frame = CGRectMake(-SreenW, 0, SreenW, SreenH);

    self.mTab.view.frame = CGRectMake(0, 0, SreenW, SreenH);

  self.pan2=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panWay:)];

  [self.mTab.view addGestureRecognizer:self.pan2];

    self.tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapWay:)];

    [self.mTab.view addGestureRecognizer:self.tap];




}


//判断平滑的方向及视图的坐标来左右视图的滑动

-(void)panWay:(UIPanGestureRecognizer *)pan

{

    CGPoint translation = [pan translationInView:pan.view];


  // NSLog(@"%f",translation.x);

    CGRect frame1=self.tbVc.view.frame;

    CGRect frame2=self.mTab.view.frame;

    if (frame1.origin.x<0 && translation.x>0){



            frame1.origin.x=frame1.origin.x+translation.x;

            //NSLog(@"farme1========%f",frame1.origin.x);

            frame2.origin.x=frame2.origin.x+translation.x;

        if (frame1.origin.x> 0) {

            frame2.origin.x=SreenW-100;

            frame1.origin.x=0;

        }

            self.tbVc.view.frame=frame1;

            self.mTab.view.frame=frame2;


        }

    if (translation.x<0 && frame2.origin.x>0 ) {

        if ([self.curretVC isKindOfClass:[TableViewController class]]) {

            frame1.origin.x=frame1.origin.x+translation.x;

            frame2.origin.x=frame2.origin.x+translation.x;

            if (frame2.origin.x<0) {

                frame2.origin.x=0;

                frame1.origin.x=-SreenW;

            }

            self.tbVc.view.frame=frame1;

            self.mTab.view.frame=frame2;


        }

        }


}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容