码迷,mamicode.com
首页 > 其他好文 > 详细

2016-1-15 抽屉效果实现demo

时间:2016-01-15 20:32:23      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ViewController.m
//  抽屉
//
//  Created by Mac on 16/1/15.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, weak) UIView *mainView;
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, assign) BOOL drag;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat scH = [UIScreen mainScreen].bounds.size.height;
    CGFloat scW = [UIScreen mainScreen].bounds.size.width;
    
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scW, scH)];
    leftView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:leftView];
    self.leftView = leftView;
    

    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scW, scH)];
    rightView.backgroundColor = [UIColor redColor];
    [self.view addSubview:rightView];
    self.rightView = rightView;
    
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scW, scH)];
    mainView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:mainView];
    self.mainView = mainView;
    
    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.mainView addGestureRecognizer:pan];
    
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
//    [self.rightView addGestureRecognizer:tap];
//    [self.leftView addGestureRecognizer:tap];
    
    [self.rightView setHidden: YES];
    [self.leftView setHidden:YES];
    
    
    
}
#pragma mark - Event after view clicked
- (void)tap:(UITapGestureRecognizer *)tapGest {
//    UIView *view = tapGest.view;
    NSLog(@"%s",__func__);
    NSLog(@"%lf",self.mainView.frame.origin.x);
    CGFloat scH = [UIScreen mainScreen].bounds.size.height;
    CGFloat scW = [UIScreen mainScreen].bounds.size.width;
    if (self.mainView.frame.origin.x != 0) {
        [UIView animateWithDuration:0.35 animations:^{
            self.mainView.frame = CGRectMake(0, 0, scW, scH);
        } completion:^(BOOL finished) {
            [self.rightView setHidden:YES];
            [self.leftView setHidden:YES];
        }];
    }
}
- (void)pan:(UIPanGestureRecognizer *)panGest
{
    CGFloat scH = [UIScreen mainScreen].bounds.size.height;
    CGFloat scW = [UIScreen mainScreen].bounds.size.width;

    
    CGPoint transform = [panGest translationInView:panGest.view];
    
    if (transform.x > 0) {//向右滑动
        if (self.leftView.isHidden) {//判断起始滑动的状态
            [self.rightView setHidden:NO];
            self.drag = NO;
            [self setupFrameWith:self.drag and:panGest]; 
        }
        if(self.rightView.isHidden && self.mainView.frame.origin.x <=0 ){// 表明用户现在在左边的界面,但是需要往回拖,也需要注意self.mainView.origin.x的大小,以防用户拖过界
            self.drag = YES;
            [self setupFrameWith:self.drag and:panGest];
        }
        
    }else if (transform .x <  0 ){//向左滑动
        if (self.rightView.isHidden) {
            [self.leftView setHidden:NO];
            self.drag = YES;
            [self setupFrameWith:self.drag and:panGest];
        }else if (self.leftView.isHidden&&self.mainView.frame.origin.x >= 0){//现在背景是rightView,且需要判断self.mianView.frame.origin.x的值,以防用户拖过界
            self.drag = NO;
            [self setupFrameWith:self.drag and:panGest];
  
        }
    }
    if (panGest.state == UIGestureRecognizerStateEnded) {// 表示手势结束
            // 先判断现在主界面的位置,然后再决定是否隐藏界面和弹回界面
        
        // 找出最大最小的x
        CGFloat maxX = CGRectGetMaxX(self.mainView.frame);
        CGFloat minX = CGRectGetMinX(self.mainView.frame);
        NSLog(@"%lf",minX);
        
        // 用户拖过时弹回
        if ( minX > 0.8*scW) {
            NSLog(@"%s",__func__);
            [UIView animateWithDuration:0.2 animations:^{
                self.mainView.frame = CGRectMake(0.8*scW, 0.4*scW, scW, scH - 0.8*scW);
            } ];
        }
        if (maxX < 0.2*scW) {
//            NSLog(@"%s",__func__);
            [UIView animateWithDuration:0.2 animations:^{
                self.mainView.frame = CGRectMake(-0.8*scW, 0.4*scW, scW, scH - 0.8*scW);
            } ];
        }
        
        
        
        if ( minX < scW / 2 && minX > 0) {//此时在右界面且需要弹回
    
         [UIView animateWithDuration:0.35 animations:^{
             self.mainView.frame = CGRectMake(0, 0, scW, scH);
         } completion:^(BOOL finished) {
             [self.rightView setHidden:YES];// 再动画完成后在隐藏;
         }];
          
        }else if(maxX > scW /2 && minX < 0){
            [UIView animateWithDuration:0.35 animations:^{
                self.mainView.frame = CGRectMake(0, 0, scW, scH);
            } completion:^(BOOL finished) {
                [self.leftView setHidden:YES];// 再动画完成后在隐藏;
            }];
        }
        


        
            
   }
        
    
    
}
- (void)setupFrameWith:(BOOL)drag and:(UIPanGestureRecognizer *)panGest
{
    CGFloat scH = [UIScreen mainScreen].bounds.size.height;
    CGFloat scW = [UIScreen mainScreen].bounds.size.width;
    // 原始frame
    CGRect frame =  self.mainView.frame;
    
    CGPoint transform = [panGest translationInView:panGest.view];
    CGFloat x = frame.origin.x+ transform.x;
    CGFloat y = frame.origin.y + transform.x/2;
    if(drag == YES){
      x = frame.origin.x+ transform.x;
      y = frame.origin.y - transform.x/2;
    }
    
    CGRect nextFrame = CGRectMake(x, y, scW, scH - y*2);
    
    self.mainView.frame = nextFrame;
    
    [panGest setTranslation:CGPointZero inView:panGest.view];
    
}
@end

从这个demo里学习巩固了很多知识,感觉好爽~~

技术分享

2016-1-15 抽屉效果实现demo

标签:

原文地址:http://www.cnblogs.com/BJTUzhengli/p/5134341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!