iOS开发时左右侧滑

作者: Hither | 来源:发表于2016-03-23 16:31 被阅读458次
    • 在我们开发中经常会遇到左右侧滑的需求,借鉴网上的资料,我整理下来,写了一个Demo;

    Category:

    
    
    .h中:
    #import <UIKit/UIKit.h>
    typedef enum{
        HYSideDirectionRight,
        HYSideDirectionLeft
    } HYSideDirection;
    
    @interface UIViewController (Side)
     //侧滑出来的View   
    @property (weak,nonatomic) UIView * sideView;
    
    //侧滑的方向,也决定了sideView是在mainPanelView 的左边还是右边    
    @property (assign,nonatomic) HYSideDirection HYSideDirectionType;
    //滑出状态
    @property (assign,nonatomic) BOOL  isSide;
    //侧滑并设置侧滑动画时间
    - (void)sideAnimateWithDuration:(NSTimeInterval)duration;
    
    @end
    
    
    .m中:
    #import "UIViewController+Side.h"
    #import <objc/runtime.h>
    //导航条高度
    const  CGFloat HYNavigationBarHeight = 64;
    
    @implementation UIViewController (Side)
    //最开始的view
    static UIView * _mainView;
    #pragma mark - 通过运行时动态添加属性
    //定义关联的Key
    static const char * sideViewKey = "sideView";
    
    - (void)setSideView:(UIView *)sideView{
        [[UIApplication sharedApplication].keyWindow addSubview:sideView];
        objc_setAssociatedObject(self, sideViewKey, sideView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        _mainView = self.navigationController?self.navigationController.view:self.view; 
    }
    
    - (UIView *)sideView{
        return  objc_getAssociatedObject(self, sideViewKey);
    }
    #pragma mark - 侧滑方向
    static const char * sideDirectionTypeKey = "sideDirectionType";
    
    - (HYSideDirection)HYSideDirectionType{  
        return  [objc_getAssociatedObject(self, sideDirectionTypeKey) intValue];
    }
    
    - (void)setHYSideDirectionType:(HYSideDirection)HYSideDirectionType{
        objc_setAssociatedObject(self, sideDirectionTypeKey, @(HYSideDirectionType), OBJC_ASSOCIATION_ASSIGN);
        CGRect rect = self.sideView.bounds;
       
        if (HYSideDirectionType == HYSideDirectionRight) {
            //右滑,滑动距离
            _sideWidth = rect.size.width;
            
            //右滑,则sideView被添加到view左边
            self.sideView.frame = CGRectMake(- rect.size.width, 0, rect.size.width, [UIScreen mainScreen].bounds.size.height);
        }else{
            
            //左滑,滑动距离
            _sideWidth = -rect.size.width;
            //左滑,则sideView被添加到view右边
            self.sideView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width , 0 , rect.size.width, rect.size.height);
        }
    }
    
    #pragma mark - 是否侧滑
    //定义关联的Key
    static const char * isSideKey = "isSide";
    
    - (BOOL)isSide{
        return  [objc_getAssociatedObject(self, isSideKey) integerValue];
    }
    
    
    - (void)setIsSide:(BOOL)isSide{
        
        objc_setAssociatedObject(self, isSideKey, @(isSide), OBJC_ASSOCIATION_ASSIGN);
    }
    
    //侧滑出来的宽度
    static CGFloat _sideWidth;
    - (void)sideAnimateWithDuration:(NSTimeInterval)duration{
        
        if (self.isSide) {   
            self.isSide = NO;
            [self hideSideViewWithDuration:(NSTimeInterval)duration];
            return;
        }
        self.isSide = YES;
        [UIView animateWithDuration:duration animations:^{
            
            _mainView.transform = CGAffineTransformMakeTranslation(_sideWidth, 0);;
            
            self.sideView.transform = CGAffineTransformMakeTranslation(_sideWidth, 0);
        }];
        
    }
    
    //侧滑时间
    - (void)hideSideViewWithDuration:(NSTimeInterval)duration{
        
        [UIView animateWithDuration:duration animations:^{
            _mainView.transform = CGAffineTransformIdentity;
            self.sideView.transform = CGAffineTransformIdentity;
        }];  
    }
    
    @end
    

    侧滑页面:

    .h中:
    #import <UIKit/UIKit.h>
    @interface MySideVC : UITableViewController
    @property (nonatomic,copy)  void(^sideblock)();
    @end
    
    .m中:
    #import "MySideVC.h"
    
    @implementation MySideVC
    
    - (void)viewDidLoad{
        
        [super viewDidLoad];
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"sideCell"];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return 20;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"sideCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor grayColor];
        cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld行",indexPath.row];
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if(self.sideblock) self.sideblock();
    }
    
    @end
    

    试图控制器

    .m中:
    #import "ViewController.h"
    #import "UIViewController+Side.h"
    #import "MySideVC.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"侧滑" style:UIBarButtonItemStylePlain target:self action:@selector(side)];
       
        MySideVC * vc = [[MySideVC alloc]init];
        //设置侧滑的view
        self.sideView = vc.view;
        vc.view.backgroundColor = [UIColor whiteColor];
        //侧滑的距离由侧滑的view的宽度决定
        vc.view.frame = CGRectMake(0, 0, 2*self.view.bounds.size.width/3, self.view.bounds.size.height);
        [self addChildViewController:vc];
        //设置侧滑view的block,什么时候隐藏侧滑的view可以由侧滑的控制器决定
        vc.sideblock = ^{
            [self side];
        };
        //侧滑的方向,向左边滑动
        self.HYSideDirectionType = HYSideDirectionLeft;
    }
    
    - (void)side{
        [self sideAnimateWithDuration:0.25];
    }
    
    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
        
        if (self.isSide) {
            [self side];
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    效果图:

    相关文章

      网友评论

      • unhangcorn:好人一生平安 :heart:
      • 乔_帮主:有demo地址吗
        乔_帮主:@写代码我负责帅 你这个方法可以控制web上有左右滑动的时候,和web上右滑手势不冲突吗?
        乔_帮主:@写代码我负责帅 OK 3q
        Hither:@佳人如荼 写一个Category将我代码复制进去。 没有Demo。不好意思哈

      本文标题:iOS开发时左右侧滑

      本文链接:https://www.haomeiwen.com/subject/vinllttx.html