美文网首页
抽屉效果

抽屉效果

作者: iOSkiller | 来源:发表于2016-05-15 21:55 被阅读0次

#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

相关文章

  • 抽屉效果

    抽屉效果拖拽 抽屉效果定位 给抽屉效果的view添加数据

  • 抽屉效果

    抽屉效果 左侧 抽屉 抽屉

  • 抽屉效果

    简洁易用的抽屉效果,一如既往的详细注解 github地址:[https://github.com/nemo316/...

  • 抽屉效果

    思路:跟控制器(rootVc)添加子控制器(下面三个)的view在跟控制器的 view 上面。(注意三个子控制器的...

  • 抽屉效果

    一.RESideMenu 现在大多App的主框架都是UITabBarController加若干导航控制器或者是带有...

  • 抽屉效果

    创建一个控制器,给控制器的view添加两个view 给上面的view添加手势,在手指拖动的时候,对上面的view做...

  • 抽屉效果

    #import "SSYViewController.h" #define ScreenW [UIScreen m...

  • 抽屉效果

    抽屉效果 效果图: 第一步:搭建界面 第二步.添加手势: 第三步:当手指松开时做到自动定位.

  • iOS 抽屉效果

    效果图 平时开发中经常会用到抽屉效果,关于抽屉的实现有许多三方库,读者可以根据需要选用,本节内容主要简单的实现一个...

  • iOS 抽屉效果

    抽屉效果思路: 三个View叠加,一个作为左View,一个作为右View,一个主View,在主View上添加拖动手...

网友评论

      本文标题:抽屉效果

      本文链接:https://www.haomeiwen.com/subject/bvavrttx.html