PUSH 转场动画

从左向右的push转场动画

- (void)animateTransition:(id)transitionContext {

UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *containerView = transitionContext.containerView;

UIView *fromView;

UIView *toView;

if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {

fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];

toView = [transitionContext viewForKey:UITransitionContextToViewKey];

} else {

fromView = fromViewController.view;

toView = toViewController.view;

}

[containerView addSubview:toView];

if ([toViewController isKindOfClass:[OneViewController class]]) {

fromView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

toView.frame = CGRectMake(-[UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

}else {

fromView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

toView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

}

[containerView addSubview:toView];

NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];

[UIView animateWithDuration:transitionDuration animations:^{

if ([toViewController isKindOfClass:[OneViewController class]]) {

fromView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

toView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

}else {

fromView.frame = CGRectMake(-[UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

toView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

}

} completion:^(BOOL finished) {

//告诉转场上下文,动画是否完成

BOOL wasCancelled = [transitionContext transitionWasCancelled];

[transitionContext completeTransition:!wasCancelled];

}];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容