iOS CATransform3D的转场动画

@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;
}
复制代码

转载于:https://juejin.im/post/5a312942518825296421b0da

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值