iOS

iOS-CATransform3D的转场动画

2016-07-06  本文已影响217人  zhf_Zachariah
@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
上一篇 下一篇

猜你喜欢

热点阅读