zoukankan      html  css  js  c++  java
  • 抽屉效果

    #import "DragerViewController.h"

     

    @interface DragerViewController ()

     

    @property (nonatomic, weakUIView *leftV;

    @property (nonatomic, weakUIView *rightV;

    @property (nonatomic, weakUIView *mainV;

     

    @end

     

    @implementation DragerViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

     

        //添加子控件

        [self setUp];

        

        

        //添加手势

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

        

        [self.mainV addGestureRecognizer:pan];

    }

     

    - (void)pan:(UIPanGestureRecognizer *)pan{

        

        //获取偏移量

        CGPoint transP = [pan  translationInView:self.mainV];

        //为什么不使用transform,是因为我们还要去修改高度,使用transform,只能修改,x,y

        //self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);

     

        self.mainV.frame = [self frameWithOffsetX:transP.x];

        //判断拖动的方向

        if(self.mainV.frame.origin.x > 0){

            //向右

            self.rightV.hidden = YES;

        }else if(self.mainV.frame.origin.x < 0){

            //向左

            self.rightV.hidden = NO;

        }

     

        //复位

        [pan setTranslation:CGPointZero inView:self.mainV];

        

    }

     

    //根据偏移量计算MainVframe

    - (CGRect)frameWithOffsetX:(CGFloat)offsetX {

        CGRect frame = self.mainV.frame;

        frame.origin.x += offsetX;

     

        return frame;

    }

     

     

     

     

    - (void)setUp{

        

        //leftV

        UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];

        leftV.backgroundColor = [UIColor blueColor];

        self.leftV = leftV;

        [self.view addSubview:leftV];

        //rightV

        UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];

        rightV.backgroundColor = [UIColor greenColor];

        self.rightV = rightV;

        [self.view addSubview:rightV];

        //mianV

        UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];

        mainV.backgroundColor = [UIColor redColor];

        self.mainV = mainV;

        [self.view addSubview:mainV];

    }

     

     

     

     

    @end

  • 相关阅读:
    第八周上机
    第七周作业
    第七周上机练习
    第六周作业
    第六次上机
    第五次上机
    第四周作业
    第四周上机练习
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5455703.html
Copyright © 2011-2022 走看看