iOS 抽屜效果

weixin_34075268發表於2017-07-29
抽屜效果思路:

三個View疊加,一個作為左View,一個作為右View,一個主View,在主View上新增拖動手勢,修改主View的frame以顯示左View和右View,設定分界值和左/右邊界值;根據響應者鏈,將主View回收的tap手勢,新增到self.view 上最合適

1913494-b80cfaea13f5f50d.gif
漸變色動畫.gif
//
//  ViewController.m
//  抽屜效果
//
//  Created by Captain on 2017/7/29.
//  Copyright © 2017年 CaptainSir. All rights reserved.
//

#import "ViewController.h"

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

@interface ViewController ()

@property (nonatomic, weak) UIView * leftView;
@property (nonatomic, weak) UIView * rightView;
@property (nonatomic, weak) UIView * mainView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addViews];
    [self setupViews];
}

- (void)addViews{
    // 左側View
    UIView * leftV = [[UIView alloc]init];
    leftV.backgroundColor = [UIColor greenColor];
    [self.view addSubview:leftV];
    
    // 右側view
    UIView * rightV = [[UIView alloc]init];
    rightV.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:rightV];
    
    // 主View
    UIView * mainV = [[UIView alloc]init];
    mainV.backgroundColor = [UIColor redColor];
    [self.view addSubview:mainV];
    
    leftV.frame = rightV.frame = mainV.frame = self.view.frame;
    self.leftView = leftV;
    self.rightView  = rightV;
    self.mainView = mainV;
}

- (void)setupViews{
    // 主View拖動手勢
    UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewPanAction:)];
    [self.mainView addGestureRecognizer:panGes];
    
    // 點選self.view tap手勢回收mainView
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mainViewRecoveryTapAction)];
    [self.view addGestureRecognizer:tapGes];
}

#define BoundaryValue      [UIScreen mainScreen].bounds.size.width * 0.5        // 位置分界值
#define TargetRight        [UIScreen mainScreen].bounds.size.width - 50        // 右側最遠距離
#define TargetLeft         50                                                   // 左側最遠距離

- (void)mainViewPanAction:(UIPanGestureRecognizer *) panGes{
    // 獲取手勢在self.mainView上的偏移量
    CGPoint point = [panGes translationInView:self.mainView];
    // 修改self.mainView的frame
    [self changeMainViewFrameWithOffsetX:point.x];
    // 復原手勢在self.mainView上的偏移量,防止疊加偏移
    [panGes setTranslation:CGPointZero inView:self.mainView];
    
    if (panGes.state == UIGestureRecognizerStateEnded) {
        // 設定邊界距離
        CGFloat margin = 0;
        if (self.mainView.frame.origin.x > BoundaryValue) {
            margin = TargetRight - self.mainView.frame.origin.x;
            
        }else if (CGRectGetMaxX(self.mainView.frame) < BoundaryValue) {
            margin = TargetLeft - CGRectGetMaxX(self.mainView.frame);
        }else {
            margin = -self.mainView.frame.origin.x;
        }
         [self changeMainViewFrameWithOffsetX: margin];
    }
}

#define MarginY     80
// 根據偏移量計算self.mainView的frame
- (void)changeMainViewFrameWithOffsetX:(CGFloat)offsetX{
    
    CGRect frame = self.mainView.frame;
    // X 值
    frame.origin = CGPointMake(frame.origin.x + offsetX, 0);
    
    // Y 值
    frame.origin.y = fabs((frame.origin.x / screenW) * MarginY);
    
    // H 值
    frame.size.height = screenH - (2 * frame.origin.y);
    

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

// 回收mainView的frame
- (void)mainViewRecoveryTapAction{
    self.mainView.frame = self.view.bounds;
}

@end

相關文章