#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运行效果图:
以上就是本文的全部内容,希望对大家学习iOS程序设计有所帮助。