iOS开发之主界面的左右滑动

发布时间:2020-06-15 23:48:30 作者:尘尘晨大飞
来源:网络 阅读:1304

前言:可能很多iOS开发者在学习之初都会对QQ的主界面的左滑与右滑动感到好奇,今天开始写我人生中的第一篇博客,即:iOS开发之Slide(主界面侧滑侧边栏)。

正文:

            首先,新建类(HomeViewController:命名随自己)继承ViewController。

            然后我们需要声明这些属性:

                

//滑动速度系数-建议在0.5-1之间。默认为0.5

@property (nonatomic, assign) CGFloat speedf;



//左侧窗控制器

@property (nonatomic, strong) UIViewController *leftVC;


//右侧滑框控制器

@property (nonatomic, strong) UIViewController *rightVC;


@property (nonatomic,strong) UIViewController *mainVC;

//点击手势控制器,是否允许点击视图恢复视图位置。默认为yes

@property (nonatomic, strong) UITapGestureRecognizer *sideslipTapGes;


//滑动手势控制器

@property (nonatomic, strong) UIPanGestureRecognizer *pan;


//侧滑窗是否关闭(关闭时显示为主页)

@property (nonatomic, assign) BOOL closed;

以及下列方法:

-(instancetype)initWithLeftView:(UIViewController *)leftVC withMainview:(UIViewController *)mainVC withRightVC:(UIViewController *)rightVC;

/**

 @brief 关闭左视图

 */

- (void)closeLeftView;

-(void)closeZPFLeftView;



/**

 @brief 打开左视图

 */

- (void)openLeftView;

-(void)showZPFleftView;

-(void)showZPFrightView;

-(void)showZPFmainView;

/**

 *  设置滑动开关是否开启

 *

 *  @param enabled YES:支持滑动手势,NO:不支持滑动手势

 */

- (void)setPanEnabled: (BOOL) enabled;

然后在.m文件中我们进行接下来的操作:

@interface LeftSlideViewController ()<UIGestureRecognizerDelegate>

{

    CGFloat _scalef;  //实时横向位移

}


@property (nonatomic,strong) UITableView *leftTableview;

@property (nonatomic,assign) CGFloat leftTableviewW;

@property (nonatomic,strong) UIView *contentView;

@property (nonatomic,strong) UIView *rightView;

@end

以及实现我们在.h文件中声明的方法:

-(instancetype)initWithLeftView:(UIViewController *)leftVC withMainview:(UIViewController *)mainVC withRightVC:(UIViewController *)rightVC {

    if (self = [super init]) {

        self.speedf = vSpeedFloat;

        

        self.leftVC = leftVC;

        self.mainVC = mainVC;

        self.rightVC = rightVC;

        

        //滑动手势

        self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(ZPFhandlePan:)];

        [self.mainVC.view addGestureRecognizer:self.pan];

        

        [self.pan setCancelsTouchesInView:YES];

        self.pan.delegate = self;

        

        self.leftVC.view.hidden = YES;

        self.rightVC.view.hidden = YES;

        

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

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

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


    }

    return self;

}


#pragma mark -- ZPF左右侧滑

-(void)ZPFhandlePan:(UIPanGestureRecognizer *)rec{

    CGPoint point = [rec translationInView:self.view];

    _scalef = (point.x*_speedf+_scalef);

    

    //根据视图位置判断是左滑还是右边滑动

    if (rec.view.frame.origin.x>=0) {

        rec.view.center = CGPointMake(rec.view.center.x+_speedf*point.x,rec.view.center.y );

        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1-_scalef/1000, 1-_scalef/1000);

        [rec setTranslation:CGPointMake(0, 0) inView:self.view];

        

        self.leftVC.view.hidden = NO;

        self.rightVC.view.hidden = YES;

        

    }else {

        rec.view.center = CGPointMake(rec.view.center.x + point.x*_speedf,rec.view.center.y);

        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1+_scalef/1000,1+_scalef/1000);

        [rec setTranslation:CGPointMake(0, 0) inView:self.view];

        

        self.rightVC.view.hidden = NO;

        self.leftVC.view.hidden = YES;

    }

    

    //手势结束后修正位置

    if (rec.state == UIGestureRecognizerStateEnded) {

        if (_scalef>140*_speedf) {

            [self showZPFleftView];

        }else if (_scalef<-140*_speedf){

            [self showZPFrightView];

        }else{

            [self showZPFmainView];

            _scalef = 0;

        }

    }

}


#pragma mark -- 单击手势

-(void)handleZPFTap:(UITapGestureRecognizer *)tap {

    if (tap.state == UIGestureRecognizerStateEnded) {

        [UIView beginAnimations:nil context:nil];

        tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

        tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);

        [UIView commitAnimations];

        _scalef = 0;

    }

}


#pragma mark -- 修改视图位置

//恢复位置

-(void)showZPFmainView {

    [UIView beginAnimations:nil context:nil];

    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.mainVC.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);

    [UIView commitAnimations];

}


//显示左视图

-(void)showZPFleftView {

    [UIView beginAnimations:nil context:nil];

    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);

    self.mainVC.view.center = CGPointMake(340,[UIScreen mainScreen].bounds.size.height/2);

    self.rightVC.view.hidden = YES;

    self.leftVC.view.hidden = NO;

    [UIView commitAnimations];

}


//显示右视图

-(void)showZPFrightView {

    [UIView beginAnimations:nil context:nil];

    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);

    self.mainVC.view.center = CGPointMake(self.view.frame.size.width*0.4,[UIScreen mainScreen].bounds.size.height/2);

    self.rightVC.view.hidden = NO;

    self.leftVC.view.hidden = YES;

    [UIView commitAnimations];

}


#pragma mark -- 隐藏左视图

-(void)closeZPFLeftView {

    [UIView beginAnimations:nil context:nil];

    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.mainVC.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);

    self.closed = YES;

    

    self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

    self.contentView.alpha = kLeftAlpha;

    

    [UIView commitAnimations];

    [self removeSingleTap];

}


#pragma mark -- 为了界面美观,可以选择隐藏或者显示状态栏

-(BOOL)prefersStatusBarHidden {

    return NO;//返回NO表示要显示,返回YES即不显示

}

至此我们已建好我们所需的左右滑动的框架。   注:(如需修改左右滑动的距离,只需修改

    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);

    self.mainVC.view.center = CGPointMake(340,[UIScreen mainScreen].bounds.size.height/2);



接下来在我们的appdelegate.m文件中进行如下操作:

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    ViewController *cc = [[ViewController alloc] init];

     self.mainNavigation = [[UINavigationController alloc] initWithRootViewController:cc];

    RightViewController *asdas = [[RightViewController alloc] init];

    asdas.view.backgroundColor = [UIColor whiteColor];

    LeftViewController *left = [[LeftViewController alloc] init];

    left.view.backgroundColor = [UIColor whiteColor];

    self.leftSlideVC = [[LeftSlideViewController alloc] initWithLeftView:left withMainview:self.mainNavigation withRightVC:asdas];

    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];


    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];


    self.window.rootViewController = self.leftSlideVC;

    

    

    // Override point for customization after application launch.

    return YES;

(可能代码比较乱,毕竟帮别人加急写出来的,望大家见谅)。

然后在我们的ViewController中需要触发左右的地方进行如下操作即可:

    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    if (isLeft == NO) {

        [app.leftSlideVC showZPFleftView];

        isLeft = YES;

    }else{

        [app.leftSlideVC closeZPFLeftView];

        isLeft = NO;

    }

注:isLeft用来判断左按钮是否按下。

好了,cmd+r   尽情的享用我们的左右滑动。

推荐阅读:
  1. ViewPager左右滑动
  2. 安卓左右滑动事件监听

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

控制器 开发者 property

上一篇:docker registry接入ceph Swift AP

下一篇:Spark1.4源码走读笔记之模式匹配

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》