抽屉效果

#import "SSYViewController.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width

#define ScreenH [UIScreen mainScreen].bounds.size.height

#define targetR 275

#define targetL -275

@interface SSYViewController ()

@property (nonatomic ,weak) UIView *leftView;

@property (nonatomic ,weak) UIView *rightView;

@property (nonatomic ,weak) UIView *midView;

@end

@implementation SSYViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self setUp];

[self addpan];

}

//添加手势

-(void)addpan{

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

[self.midView addGestureRecognizer:pan];

}

//监听者调用手势执行的方法

-(void)pan:(UIPanGestureRecognizer *)pan{

CGPoint transP = [pan translationInView:self.midView];

self.midView.frame = [self framWithOffsetX:transP.x];

if(pan.state == UIGestureRecognizerStateEnded){

//自动定位

CGFloat target = 0;

//1.当mainV的x大于屏幕宽度一半时,自动定位到右侧

if (self.midView.frame.origin.x > ScreenW * 0.5) {

//自动定位到右侧

target = targetR;

}else if(CGRectGetMaxX(self.midView.frame) < ScreenW * 0.5){

//2.当mainV的最大的X值,小于屏幕,宽度一半时,自动定位到左侧

target = targetL;

}

CGFloat offsetX = target - self.midView.frame.origin.x;

[UIView animateWithDuration:0.5 animations:^{

self.midView.frame =  [self framWithOffsetX:offsetX];

}];

}

[pan setTranslation:CGPointZero inView:self.midView];

}

//计算移动后的尺寸

-(CGRect)framWithOffsetX:(CGFloat)offsetX{

CGRect frame = self.midView.frame;

frame.origin.x += offsetX;

frame.origin.y = fabs(frame.origin.x * 100 /[UIScreen mainScreen].bounds.size.width);

frame.size.height = [UIScreen mainScreen].bounds.size.height - 2*frame.origin.y;

self.midView.frame = frame;

if (self.midView.frame.origin.x > 0) {

self.rightView.hidden = YES;

}else if(self.midView.frame.origin.x < 0){

self.rightView.hidden = NO;

}

return frame;

}

//设置界面

-(void)setUp{

//添加左边的view

UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];

leftView.backgroundColor = [UIColor redColor];

self.leftView = leftView;

[self.view addSubview:leftView];

//添加右边的view

UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];

rightView.backgroundColor = [UIColor blueColor];

self.rightView = rightView;

[self.view addSubview:rightView];

//添加中间的view

UIView *midView = [[UIView alloc] initWithFrame:self.view.bounds];

midView.backgroundColor = [UIColor purpleColor];

self.midView = midView;

[self.view addSubview:midView];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • //定义属性宏 #define LWKeyPath(objc, keyPath) @(((void)objc.ke...
    年轻在于折腾阅读 1,889评论 0 3
  • 一.RESideMenu 现在大多App的主框架都是UITabBarController加若干导航控制器或者是带有...
    laitys阅读 824评论 0 4
  • 目前市场上很多APP都有抽屉效果的界面,界面大同小异,一下是我个人分析和实现的抽屉效果,我是以代码加注释的方式分析...
    coderwx阅读 763评论 0 0
  • 自己写的简单抽屉效果,大概原理都是一样的,直接放代码。 #import"DrawViewController.h"...
    BEYOND黄阅读 417评论 0 3
  • 思路:跟控制器(rootVc)添加子控制器(下面三个)的view在跟控制器的 view 上面。(注意三个子控制器的...
    码农耕阅读 373评论 0 0