首页 技术 正文
技术 2022年11月13日
0 收藏 891 点赞 3,611 浏览 3495 个字

简单抽屉效果的实现

就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等。所以,今天我们就来简单的实现一下。当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一些功夫了,我们这里知识简单的介绍怎么去实现,不过一般我们开发都是找别人做好的,也没必要烂肺时间,除非你真的是大牛或者闲的蛋疼。

其实关于抽屉效果就是界面有三个View,其实一个主View其他两个分别是左边和右边的View,我们分别为他们添加手势,实现左右滑动显示对应的View。

一:所以,首先我们需要在头文件中定义三个View的属性,来给外界调用,实现设置对应的属性和效果:

 @property (nonatomic, weak, readonly) UIView *mainV; @property (nonatomic, weak, readonly) UIView *leftV; @property (nonatomic, weak, readonly) UIView *rightV;

二:然后在实现文件中定义对应的宏,后面要用到:

 // @"frame" #define XMGkeyPath(objc, keyPath) @(((void)objc.keyPath, #keyPath)) // 在宏里面如果在参数前添加了#,就会把参数变成C语言字符串 // 获取屏幕的宽度 #define screenW [UIScreen mainScreen].bounds.size.width // 获取屏幕的高度 #define screenH [UIScreen mainScreen].bounds.size.height

相信这里有一个宏你们一定很陌生,由于本章主要介绍抽屉效果,宏不是本章的内容,如果你真的想了解或者不能看懂上面的含义清看大神王魏分享的:

宏定义的黑魔法 – 宏菜鸟起飞手册

三:在ViewDidLoad中调用下面这个方法实现三个View的创建:

 // 添加所有的子控件 - (void)setUpAllChildView {     // left     UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];     leftV.backgroundColor = [UIColor greenColor];     [self.view addSubview:leftV];     _leftV = leftV;     // right     UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];     rightV.backgroundColor = [UIColor blueColor];     [self.view addSubview:rightV];     _rightV = rightV;     // main     UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];     mainV.backgroundColor = [UIColor redColor];     [self.view addSubview:mainV];     _mainV = mainV; }

四:在ViewDidLoad中为界面添加滑动手势:

     // 添加拖拽手势     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];     [_mainV addGestureRecognizer:pan]; 

并且使用KVO监听frame的变化:

注意:这里是整篇文章中最重的地方,也是相对最难的地方,主要设置到的一些计算

     // KVO作用:时刻监听某个对象的某个属性的改变     // _main frame属性的改变     // Observer:观察者     // KeyPath:监听的属性     // NSKeyValueObservingOptionNew:表示监听新值的改变     [_mainV addObserver:self forKeyPath:XMGkeyPath(_mainV, frame) options:NSKeyValueObservingOptionNew context:nil];     

五:实现滑动手势的方法,和监听属性变化的方法:

 #define targetR 300 #define targetL -200 - (void)pan:(UIPanGestureRecognizer *)pan {     // 获取手势的偏移量     CGPoint transP = [pan translationInView:_mainV];     // 获取x轴的偏移量,相对于上一次     CGFloat offsetX = transP.x;     // 修改最新的main.frame,     _mainV.frame = [self frameWithOffsetX:offsetX];     // 复位     [pan setTranslation:CGPointZero inView:_mainV];     // 判断下当前手指有没有抬起,表示手势结束     if (pan.state == UIGestureRecognizerStateEnded) { // 手指抬起,定位         // x>屏幕的一半,定位到右边某个位置         CGFloat target = ;         if (_mainV.frame.origin.x > screenW * 0.5) {             target = targetR;         }else if (CGRectGetMaxX(_mainV.frame) < screenW * 0.5){             // 最大的x < 屏幕一半的时候,定义到左边某个位置             target = targetL;         }         // 获取x轴的偏移量         CGFloat offsetX = target - _mainV.frame.origin.x;         [UIView animateWithDuration:0.25 animations:^{             _mainV.frame = [self frameWithOffsetX:offsetX];         }];     } } #define XMGMaxY 100 // 给定一个x轴的偏移量计算下最新main的frame - (CGRect)frameWithOffsetX:(CGFloat)offsetX {     // 获取当前main的frame     CGRect frame = _mainV.frame;     // 计算当前的x,y,w,h     // 获取最新的x     CGFloat x = frame.origin.x + offsetX;     // 获取最新的y     CGFloat y = x / screenW * XMGMaxY;     // 当用户往左边移动的时候,_main.x < 0,y需要增加,为正     ) {         y = -y;     }     // 获取最新的h     CGFloat h = screenH -  * y;     // 获取缩放比例     CGFloat scale = h / screenH;     // 获取最新的w     CGFloat w = screenW * scale;     return CGRectMake(x, y, w, h); }

属性监听方法:

 // 只要监听的属性一改变,就会调用 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     ) { // 往右滑动,显示左边控件,隐藏右边控件         _rightV.hidden = YES;     }){ // 往左滑动,显示右边控件         _rightV.hidden = NO;     } }

注意:还记得我们前面学通知的时候一般用完都会讲通知移除,这里的KVO也是同样,所以我就提前做了,方便后面忘了(实际开发中也是一样的)

 // 注意:当对象被销毁的时候,一定要注意移除观察者 - (void)dealloc {     // 移除观察者     [_mainV removeObserver:self forKeyPath:XMGkeyPath(_mainV, frame)]; }

六:为界面添加一个Tap手势,为了实现点一下屏幕还原

      // 给控制器的view添加一个点按      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];      [self.view addGestureRecognizer:tap];

还原方法的实现:

 - (void)tap {     ) {         // 把_mainV还原最开始的位置         [UIView animateWithDuration:0.25 animations:^{             _mainV.frame = self.view.bounds;         }];     } }

下面就是最重的效果:

iOS开发——实用技术OC篇&简单抽屉效果的实现     

iOS开发——实用技术OC篇&简单抽屉效果的实现

iOS开发——实用技术OC篇&简单抽屉效果的实现

iOS开发——实用技术OC篇&简单抽屉效果的实现

iOS开发——实用技术OC篇&简单抽屉效果的实现

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,114
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,586
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,432
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,924