iOS最简单的抽屉效果

1.storyboard的布局:

2.ViewController里的实现:

#import "ViewController.h"

@interface ViewController ()
//拖滑动的UIView的三个约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topconstant;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthconstant;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomconstant;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
//左滑事件
- (IBAction)leftswipe:(id)sender {
    //添加Container的UIView滑动全屏的约束
    self.topconstant.constant = 0;
    self.bottomconstant.constant = 0;
    self.widthconstant.constant = self.view.bounds.size.width;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];  //更新UI
}


//右滑动
- (IBAction)rightswipe:(id)sender {
    //还远Container的UIView滑动的初始约束
    self.topconstant.constant = 40;
    self.bottomconstant.constant = 40;
    self.widthconstant.constant = 150;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];  //更新UI
}



@end

3.最终效果:


4.在csdn上的代码仓库是:https://code.csdn.net/hongdeng123/ios_drawerresult/tree/master


猜你喜欢

转载自blog.csdn.net/hongdeng123/article/details/52828854