2019独角兽企业重金招聘Python工程师标准>>>

在显示在窗口的控制器上添加三个view(如果只需要往一边滑动就只加2个view)

先声明三个view

#import "ViewController.h"@interface ViewController ()
@property(nonatomic, weak) UIView *mainV;
@property(nonatomic, weak) UIView *leftV;
@property(nonatomic, weak) UIView *rightV;
@end

添加view到控制器view上

#pragma mark - 添加子控件
- (void)setUpChildViews {UIView *leftV = [[UIView alloc]initWithFrame:self.view.bounds];leftV.backgroundColor = [UIColor orangeColor];[self.view addSubview:leftV];_leftV = leftV;UIView *rightV = [[UIView alloc]initWithFrame:self.view.bounds];rightV.backgroundColor = [UIColor groupTableViewBackgroundColor];[self.view addSubview:rightV];_rightV = rightV;UIView *mainV = [[UIView alloc]initWithFrame:self.view.bounds];mainV.backgroundColor = [UIColor yellowColor];[self.view addSubview:mainV];_mainV = mainV;
}- (void)viewDidLoad {[super viewDidLoad];//添加子控件[self setUpChildViews];//添加Pan手势UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];[self.view addGestureRecognizer:pan];//添加点按手势,在主视图上任意位置点击回到屏幕开始位置UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];[self.view addGestureRecognizer:tap];}
#pragma mark - 手势识别方法
#define targetL -230
#define targetR 200
#define screenW [UIScreen mainScreen].bounds.size.width
- (void)pan:(UIPanGestureRecognizer *)pan {//获取手势移动的位置CGPoint tranP = [pan translationInView:self.view];//获取x的偏移量CGFloat offsetX = tranP.x;//修改mainV的frame_mainV.frame = [self frameWithOffsetX:offsetX];//判断mainV的x是否大于0[self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];//复位[pan setTranslation:CGPointZero inView:self.view];//判断当手势结束的时候,定位if (pan.state == UIGestureRecognizerStateEnded) {CGFloat target = 0;if (_mainV.frame.origin.x > screenW * 0.5) {//定位到右边target = targetR;}else if(CGRectGetMaxX(_mainV.frame) < screenW * 0.5) {//定位到左边target = targetL;}//获取X轴需要移动的偏移量CGFloat offsetX = target - _mainV.frame.origin.x;[UIView animateWithDuration:0.25 animations:^{_mainV.frame = target == 0 ? self.view.bounds : [self frameWithOffsetX:offsetX];}];}}
- (void)tap {[UIView animateWithDuration:0.25 animations:^{_mainV.frame = self.view.bounds;}];}
#define kMaxY 80
#pragma mark - 根据offsetX计算mainV的frame
- (CGRect)frameWithOffsetX:(CGFloat)offsetX {//获取上一次的frameCGRect frame = _mainV.frame;//获取屏幕的高度CGFloat screenH = [UIScreen mainScreen].bounds.size.height;//获取屏幕的宽度//CGFloat screenW = [UIScreen mainScreen].bounds.size.width;//X轴平移一点对应Y轴需要平移的距离CGFloat offsetY = offsetX * kMaxY / screenW;//获取上一次的高度CGFloat preH = frame.size.height;//获取上一次的宽度CGFloat preW = frame.size.width;//获取当前高度CGFloat curH = preH - 2 * offsetY;//如果是向左滑动if(frame.origin.x < 0) {curH = preH + 2 * offsetY;}//获取尺寸的缩放比例CGFloat scale = curH / preH;//获取当前宽度CGFloat curW = preW * scale;//获取当前xframe.origin.x += offsetX;//获取当前yCGFloat y = (screenH - curH) / 2;frame.origin.y = y;frame.size.width = curW;frame.size.height = curH;return frame;}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {if(_mainV.frame.origin.x > 0) {//往右边滑动_rightV.hidden = YES;}else if(_mainV.frame.origin.x < 0) {//往左边滑动_rightV.hidden = NO;}}

如果想要在mainV主视图中显示tableView,就新创建一个TableViewController,在这里面显示tableView的数据,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 30;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];if(cell == nil) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];return cell;
}

再创建一个在storyboard中显示的控制器XXMainViewController,继承自实现了抽屉效果的ViewController,并且在storyboard中将class改为这个控制的类名,还要将显示tableView的控制成为他的子控制器

- (void)viewDidLoad {[super viewDidLoad];XXViewController *vc = [[XXViewController alloc]init];vc.view.frame = self.view.bounds;//让vc成为主视图控制器的子控制器[self addChildViewController:vc];//主视图展示tableView[self.mainV addSubview:vc.view];}

为了在XXMainViewController中只能访问mainV而不能修改他的值,所以将子控件的view暴露在ViewController.h中,并添加read-only

#import <UIKit/UIKit.h>@interface ViewController : UIViewController
@property(nonatomic, weak, readonly) UIView *mainV;
@property(nonatomic, weak, readonly) UIView *leftV;
@property(nonatomic, weak, readonly) UIView *rightV;
@end

运行效果图:

转载于:https://my.oschina.net/shenhuniurou/blog/651928

iOS开发之抽屉效果相关推荐

  1. iOS开发之抽屉效果实现

    说道抽屉效果在iOS中比较有名的第三方类库就是PPRevealSideViewController.一说到第三方类库就自然而然的想到我们的CocoaPods,今天的博客中用CocoaPods引入PPR ...

  2. iOS开发手机震动效果

    首先要导入 #import <AudioToolbox/AudioToolbox.h> 一行代码搞定: AudioServicesPlaySystemSound(kSystemSoundI ...

  3. ios开发学习-手势交互(Gesture)效果源码分享

    qianqianlianmeng ios开发学习-手势交互(Gesture)效果源码分享 All Around Pull View 介绍:实现视图四个方向(上下左右)都能够拖动更新(pull to r ...

  4. iOS开发 QQ粘性动画效果

    QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00  博客园精华区 原文  http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...

  5. iOS开发之各种动画各种页面切面效果

    转发:http://www.cocoachina.com/ios/20141226/10775.html 今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发 ...

  6. android 3d侧拉抽屉,iOS动画指南 - 4.右拉的3D抽屉效果

    一切的动画其实都是假象,3D效果也是这样.本篇我们来做一个这样的特效. 先忽略掉3D效果,我们先要做的是一个右拉的抽屉效果. 总体思路: 1.创建一个ContainerViewController容器 ...

  7. 安卓开发SlidingDrawer实现抽屉效果

    在手机开发中,我们会遇到这样的问题,要在手机屏幕上显示很多信息,但是手机屏幕就那么小一点,当内容较多的时候如何显示呢,我们如何理用更有限的空间来显示更多的信息呢?我们可以使用安卓系统提供的Slidin ...

  8. 2.7 实现强大的单元格左右滑动效果并添加功能按钮 [iOS开发中的神兵利器]

    原文:http://coolketang.com/staticCoding/5a995103d50eee2ea30512f9.html 1. 您将在本节课对单元格进行扩展,当鼠标在单元格左右滑动时,弹 ...

  9. 防ios的抽屉效果,防qq的抽屉效果

    AS中导入GitHub开源项目SlidingMenu总结,我开始AS导入SlidingMenu的时候也百度了很多文章,写的都不是很详细,所以导入成功后,写了这篇文章,希望对想用AndroidStudi ...

最新文章

  1. oracle挂堎,Oracle 冷拷备实例挂到新ORACLE时应注意问题。
  2. 搭建自己的前端自动化测试脚手架(三)
  3. 残缺棋盘问题算法分析_javascript使用递归回溯算法和贪心算法解决马踏棋盘问题...
  4. wifi一键配网smartconfig原理及应用
  5. JavaScript变量作用域和内存问题(js高级程序设计总结)
  6. 可真有你的!EasyEdge模型压缩新姿势……
  7. java.util.function包
  8. 使用UIBinder的GWT自定义按钮
  9. 以实时风控场景为例,阿里云实时计算如何来做异常检测?
  10. EL表达式及其定义和使用 转
  11. 更改自身web项目的图标(默认为tomcat的小喵咪)
  12. 关于wait、notify在火车售票战的实例
  13. 移动磁盘修复、格式化【微PE】
  14. matlab把结构按条件排序,matlab结构体数组排序
  15. WordPress整站轻松开启HTTPS 1
  16. iis服务器网站启动不了,IIS上打不开asp网站怎么办
  17. VB.NET 保留2位小数
  18. uni-app 输入框类型
  19. gnuplot(九)、gnuplot画矢量与颜色设置
  20. 基于 Fortran QuickWin 的物性计算应用程序开发示例

热门文章

  1. DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016错误解决方法
  2. 解决ADB启动问题(Failed to initialize Monitor Thread: Unable to establish loopback connection)
  3. 解决Eclipse无法添加Tomcat服务器的问题
  4. 数据库主从数据一致性的几种解决方案
  5. 启动mysql时显示:/tmp/mysql.sock 不存在的解决方法
  6. 解决sublime text 3使用Install Package时出现There are no packages available for installation问题
  7. 在Python中将列表转换为元组
  8. ubuntu18.04下 c++安装opencv-3.4.6,c++安装opencv-3.4.9,clion配置opencv-3.4.6与 python安装 opencv-3.4.6
  9. maven构建ssm工程
  10. 正则表达式查找字符串_如何简单理解正则表达式?只需1分钟就可以看到她优美的舞姿...