transitionContext获取到起始和目标控制。并在containView这个视图实现我们想要实现的动画。
接下来就是转场动画的实现了,push动画的流程大概就是:点击第一个页面的一个控件,模拟出控件然后移动到第二个界面同一个样式控件的位置,之后再把第二个界面以控件的位置中心扩散显示出来。
那么现在我们就在协议方法中通过fromVC获取到第一个视图的控件,并制造一个镜像视图移动到toVC的目标控件的位置。在这里,我调用了UIViewcontroller分类关联一个对象,用来记录控件(非拥有关系)。
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface UIViewController (SFTrainsitionExtension)
@property (assign, nonatomic) CGFloat sf_targetHeight;//灰白背景的分割线高度
@property (weak , nonatomic) UIView *sf_targetView;
@end
产生targetView镜像:
//产生targetView镜像
- (UIView *)customSnapshoFromView:(UIView *)inputView {
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create an image view.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(0.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
return snapshot;
}
现在就可以制作移动的动画:
//起始位置
CGRect originFrame = [fromVC.sf_targetView convertRect:fromVC.sf_targetView.bounds toView:fromVC.view];
//动画移动的视图镜像
UIView *customView = [self customSnapshoFromView:fromVC.sf_targetView];
customView.frame = originFrame;
//移动的目标位置
CGRect finishFrame = [toVC.sf_targetView convertRect:toVC.sf_targetView.bounds toView:toVC.view];
UIView *containView = [transitionContext containerView];
//背景视图 灰色高度
CGFloat height = CGRectGetMidY(finishFrame);
toVC.sf_targetHeight = height;
//背景视图 灰色
UIView *backgray = [[UIView alloc] initWithFrame:CGRectMake(0, 0, k_SF_SCREEN_WIDTH, k_SF_SCREEN_HIGHT)];
backgray.backgroundColor = [UIColor lightGrayColor];
//背景视图 白色
UIView *backwhite = [[UIView alloc] initWithFrame:CGRectMake(0, height, k_SF_SCREEN_HIGHT, k_SF_SCREEN_HIGHT-height)];
backwhite.backgroundColor = [UIColor whiteColor];
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
//注意添加顺序
[containView addSubview:toVC.view];
[containView addSubview:backgray];
[backgray addSubview:backwhite];
[containView addSubview:customView];
//动画
[UIView animateWithDuration:_duration/3 animations:^{
customView.frame = finishFrame;
customView.transform = CGAffineTransformMakeScale(1.1, 1.1);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:_duration/3 animations:^{
customView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:_duration/3 animations:^{
customView.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[backgray removeFromSuperview];
[customView removeFromSuperview];
[transitionContext completeTransition:YES];
}
}];
[self addPathAnimateWithView:backgray fromPoint:customView.center];
}
}];










