iOS CATransform3D的轉場動畫

zhf_Zachariah發表於2017-12-13
@property (nonatomic,strong) UIView *maskLayer;//遮罩層
@property (nonatomic,strong) UIView *popView;//底部pop的view

//第一次轉換
- (CATransform3D)getFirstTransform{
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0 / -900.0;
    transform = CATransform3DScale(transform, 0.95, 0.95, 1.0);
    transform = CATransform3DRotate(transform, (CGFloat)(15.0*M_PI/180.0), 1, 0, 0);
    transform = CATransform3DTranslate(transform, 0, 0, -55.0);
    return transform;
}
//第二次轉換
- (CATransform3D)getSecondTransform{
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = [self getFirstTransform].m34;
    transform = CATransform3DTranslate(transform, 0, self.view.frame.size.height * -0.08,0);
    transform = CATransform3DScale(transform, 0.8, 0.8, 1.0);
    return transform;
}
//開始動畫
- (void)openAnimation{
    [self.view addSubview:self.maskLayer];
    [[UIApplication sharedApplication].keyWindow addSubview:self.popView];
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor blackColor];
    CGRect rect = self.popView.frame;
    rect.origin.y = kScreenHeight / 3  * 2;
    [UIView animateWithDuration:0.2 animations:^{
        self.view.layer.transform = [self getFirstTransform];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 animations:^{
            self.view.layer.transform = [self getSecondTransform];
            self.maskLayer.alpha = 0.5;
            self.popView.frame = rect;
        }];
    }];
}
//關閉動畫
- (void)closeAnimation{
    CGRect rect = self.popView.frame;
    rect.origin.y = kScreenHeight / 2 + rect.size.height + 49;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.layer.transform = [self getFirstTransform];
        self.popView.frame = rect;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.view.layer.transform = CATransform3DIdentity; 
            self.maskLayer.alpha = 0;
        } completion:^(BOOL finished) {
            [self.maskLayer removeFromSuperview];
            [self.popView removeFromSuperview];
            [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
        }];
    }];
}
- (void)close{
    [self closeAnimation];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
//遮罩層
- (UIView *)maskLayer{
    if (!_maskLayer) {
        _maskLayer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
        _maskLayer.backgroundColor = [UIColor blackColor];
        _maskLayer.alpha = 0.7;
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [_maskLayer addSubview:effectview];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = _maskLayer.bounds;
        [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
        [_maskLayer addSubview:button];
    }
    return _maskLayer;
}
- (UIView *)popView{
    if (!_popView) {
        _popView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight / 3)];
        _popView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    }
    return _popView;
}
複製程式碼

動畫效果.gif

相關文章