最简单的view动效

76 阅读1分钟
#import "ViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define toolH 88

@interface ViewController ()

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, assign) BOOL toolViewShow;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    self.toolViewShow = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if(self.toolViewShow) {
        //显示了则退回去
        [UIView animateWithDuration:0.25 animations:^{
//            self.topView.transform = CGAffineTransformIdentity;
//            self.bottomView.transform = CGAffineTransformIdentity;

            self.topView.frame = CGRectMake(0, -(toolH), kScreenW, toolH);
            self.bottomView.frame = CGRectMake(0, kScreenH, kScreenW, toolH);
        } completion:^(BOOL finished) {
            self.topView.hidden = YES;
            self.bottomView.hidden = YES;
        }];
    } else {
        //没显示则显示出来
        self.topView.hidden = NO;
        self.bottomView.hidden = NO;
        [UIView animateWithDuration:0.25 animations:^{
//            self.topView.transform = CGAffineTransformMakeTranslation(0, toolH);
//            self.bottomView.transform = CGAffineTransformMakeTranslation(0, -(toolH));

            self.topView.frame = CGRectMake(0, 0, kScreenW, toolH);
            self.bottomView.frame = CGRectMake(0, kScreenH-toolH, kScreenW, toolH);
        }];
    }
    self.toolViewShow = !self.toolViewShow;
}

- (UIView *)topView {
    if(_topView == nil) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, -(toolH), kScreenW, toolH)];
        _topView.hidden = YES;
        _topView.backgroundColor = [UIColor orangeColor];
    }
    return _topView;
}

- (UIView *)bottomView {
    if(_bottomView == nil) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH, kScreenW, toolH)];
        _bottomView.hidden = YES;
        _bottomView.backgroundColor = [UIColor redColor];
    }
    return _bottomView;
}

@end