码迷,mamicode.com
首页 > 其他好文 > 详细

抽屉效果

时间:2016-05-03 17:53:26      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

#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

抽屉效果

标签:

原文地址:http://www.cnblogs.com/liuzhenjie/p/5455703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!