iOS实现抽屉效果

这篇文章主要为大家详细介绍了iOS实现抽屉效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现抽屉效果的具体代码,供大家参考,具体内容如下

抽屉效果:

 #import "DragerViewController.h" #define screenW [UIScreen mainScreen].bounds.size.width @interface DragerViewController () @property (nonatomic, weak) UIView *leftV; @property (nonatomic, weak) UIView *rightV; @property (nonatomic, weak) UIView *mainV; @end @implementation DragerViewController - (void)viewDidLoad { [super viewDidLoad]; //添加子控件 [self setUp]; //添加手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.mainV addGestureRecognizer:pan]; //给控制器的View添加点按手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; [self.view addGestureRecognizer:tap]; } - (void)tap{ //让MainV复位 [UIView animateWithDuration:0.5 animations:^{ self.mainV.frame = self.view.bounds; }]; } #define targetR 275 #define targetL -275 - (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; } //当手指松开时,做自动定位. CGFloat target = 0; if (pan.state == UIGestureRecognizerStateEnded) { if (self.mainV.frame.origin.x > screenW * 0.5 ) { //1判断在右侧 //当前View的x有没有大于屏幕宽度的一半,大于就是在右侧 target = targetR; }else if(CGRectGetMaxX(self.mainV.frame) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是iOS实现抽屉效果的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动