抽屉效果代码

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"

#define targetR 250
#define targetL -250

#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

/** 红色的view*/
@property (nonatomic, weak) UIView *mainView;
/** 左边的view*/
@property (nonatomic, weak) UIView *leftView;
/** 右边的view*/
@property (nonatomic, weak) UIView *rightView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加子视图
    [self addViews];
    // 给self.view添加一个手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
}

- (void)tap:(UITapGestureRecognizer *)tap
{

    [UIView animateWithDuration:0.5 animations:^{
        self.mainView.frame = self.view.bounds;
    }];
}

// 添加子视图
- (void)addViews
{
    // 创建一个蓝色view
    UIView *blueView = [[UIView alloc] init];
    blueView.frame = self.view.bounds;
    blueView.backgroundColor = [UIColor blueColor];
    self.leftView = blueView;
    [self.view addSubview:blueView];

    // 创建一个绿色view
    UIView *greenView = [[UIView alloc] init];
    greenView.frame = self.view.bounds;
    greenView.backgroundColor = [UIColor greenColor];
    self.rightView = greenView;
    [self.view addSubview:greenView];

    // 创建一个红色view
    UIView *mainView = [[UIView alloc] init];
    mainView.frame = self.view.bounds;
    mainView.backgroundColor = [UIColor redColor];
    self.mainView = mainView;
    [self.view addSubview:mainView];
    // 给mainView添加拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.mainView addGestureRecognizer:pan];
}

// 拖动手势
- (void)pan:(UIPanGestureRecognizer *)pan
{
    // 拿到手势当前点,pan.view就是self.mainView
    CGPoint transP = [pan translationInView:pan.view];

    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, transP.x, transP.y);

    //根据传入的偏移量计算self.mainV的frame
    self.mainView.frame = [self frameWithOffset:transP.x];

    if (self.mainView.frame.origin.x > 0) {
        self.rightView.hidden = YES;
    } else if (self.mainView.frame.origin.x < 0) {
        self.rightView.hidden = NO;
    }

    // 拖动过程没有松开手指,所以不会调用下面的方法,但是self.mainView的尺寸一直在改变
    // 所以传人到里面的self.mainView也是改变了的
    if (pan.state == UIGestureRecognizerStateEnded) {
        CGFloat target = 0;
        // 下面两种情况才会改变target的值,其他情况target都为0
        // 比如self.mainView.frame.origin.x < screenW * 0.5
        // 比如 CGRectGetMaxX(self.mainView.frame) > screenW * 0.5
        if (self.mainView.frame.origin.x > screenW * 0.5) {
            target = targetR;
        } else if (CGRectGetMaxX(self.mainView.frame) < screenW * 0.5) {
            target = targetL;
        }

        CGFloat offset = target - self.mainView.frame.origin.x;
        // 同样根据传入的偏移量计算self.mainV的frame
        [UIView animateWithDuration:0.5 animations:^{
            self.mainView.frame = [self frameWithOffset:offset];
        }];

    }

    //复位
    [pan setTranslation:CGPointZero inView:pan.view];
}

//根据传入的偏移量计算self.mainV的frame
- (CGRect)frameWithOffset:(CGFloat)offset
{
    CGRect frame = self.mainView.frame;
    // offset刚刚自己打印了一些,就是连续输出一串0.5。。。。1。。。很小的一些数
    // 所以要想真正获得self.mainView的真正位置,必须
    frame.origin.x += offset;

    frame.origin.y = fabs(100 * frame.origin.x / screenW);
    frame.size.height = screenH - 2 * frame.origin.y;

    return frame;
}

@end

程序一开始效果图片:

红色view的x大于屏幕宽度的一半后显示的效果图片:

红色view的x小于屏幕宽度的一半后显示的效果图片:

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

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,498评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 在堆里面存放着Java世界几乎所有的对象实例,垃圾收集器在对堆进行回收前,首先要确定对象的“存活”与“死去”,这就...
    进击de小黑阅读 523评论 1 1