您的位置:首页 > 其它

[10秒学会] - 自定义专场动画

2016-09-11 00:00 381 查看
第一步: 设置转场

DCViewController *dcVC = [[DCViewController alloc]init];
[dcVC setModalPresentationStyle:UIModalPresentationCustom]; //自己定义专场动画
dcVC.transitioningDelegate = self.dcAnimator; //代理
[self presentViewController:dcVC animated:YES completion:^{
}];


第二步:代理

/** 改变弹出view的尺寸 */
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
_isPresent = YES;
return [[DCPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
}

/** 改变弹出前的代理 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
_isPresent = NO;
return self;
}

/** 弹出后的代理*/
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
return self;
}

/** 弹出后的时间*/
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1;
}

/** 显示的动画 */
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
//获取父view
UIView *containerView = [transitionContext containerView];
if(_isPresent){ //显现
DCViewController *fromViewController = (DCViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[containerView addSubview:fromViewController.view];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
fromViewController.view.transform = CGAffineTransformMakeRotation(180 * (M_PI / 180.0f));
[UIView commitAnimations];
}else{ // 消失

}
}

第三部:改变显示的内容 & frame 可任意定义

@implementation DCPresentationController

- (void)containerViewWillLayoutSubviews {
[super containerViewWillLayoutSubviews];

//添加蒙版
[self.containerView insertSubview:self.bgView atIndex:0];
self.bgView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
self.bgView.frame = self.containerView.bounds;
self.presentedView.frame = CGRectMake(0, 0, 100, 100);
}

- (UIView *)bgView{
if(!_bgView){
_bgView = [[UIView alloc]init];

}
return _bgView;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义专场动画