iOS 100行代碼簡單實(shí)現(xiàn)抽屜效果

最近看到自己一些網(wǎng)上的抽屜效果,看起來很酷!很眩!
但是,下下來看代碼, 密密麻麻的,唉!!算了, 還是自己寫吧!!


效果圖.gif
#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController;
@end

#import "MainViewController.h"

#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height

@interface MainViewController ()
@property (nonatomic,strong)UIViewController *leftVC;
@property (nonatomic,strong)UIViewController *centerVC;
@property (nonatomic,assign)BOOL isSlider;
@property (nonatomic,strong)UIView *corverView;
@end

@implementation MainViewController
+ (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController{

    MainViewController *mainVC = [[MainViewController alloc] init];
    mainVC.leftVC = leftViewController;
    mainVC.centerVC = mainPageViewController;
    return mainVC;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.isSlider = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self addChildViewController:self.leftVC];
    [self.view addSubview:self.leftVC.view];
    [self addChildViewController:self.centerVC];
    [self.view addSubview:self.centerVC.view];
    // 給左視圖和主視圖添加手勢
    [self addGestureForView];
}
// 給主視圖添加遮蓋
- (void)addCorverView{
    if (self.corverView) {
        [self.corverView removeFromSuperview];
        self.corverView = nil;
    }
    self.corverView = [[UIView alloc] initWithFrame:self.centerVC.view.bounds];
    _corverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    [self.centerVC.view addSubview:self.corverView];
}
// 移除主視圖遮蓋
- (void)removeCoverView{
    if (self.corverView) {
        [self.corverView removeFromSuperview];
        self.corverView = nil;
    }
}
// 給左視圖和主視圖添加手勢
- (void)addGestureForView{
    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.centerVC.view addGestureRecognizer:rightGesture];
    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.centerVC.view addGestureRecognizer:leftGesture];
    UISwipeGestureRecognizer *leftVCLeftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftVCLeftSwipeAction:)];
    leftVCLeftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.leftVC.view addGestureRecognizer:leftVCLeftSwipeGesture];
}
- (void)swipeRightAction:(id)sender{
    [self moveView:self.centerVC.view scale:0.8 panValue:KWidth];
    self.isSlider = YES;
    [self addCorverView];
}
- (void)swipeLeftAction:(id)sender{
    [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
    self.isSlider = NO;
    [self removeCoverView];
}
- (void)leftVCLeftSwipeAction:(id)sender{
    [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2];
    self.isSlider = NO;
    [self removeCoverView];
}
// 平移和縮放一個(gè)視圖
- (void)moveView:(UIView *)view scale:(CGFloat)scale panValue:(CGFloat)value{
    [UIView beginAnimations:nil context:nil];
    view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale);
    view.center = CGPointMake(value, view.center.y);
    [UIView commitAnimations];
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容