然后创建子类PDAnimatorPUshPopTransition继承自PDAnimatorBaseTransition,实现- (void)animationPush,- (void)animationPop方法。
PDAnimatorPUshPopTransition.h
#import "PDAnimatorBaseTransition.h"
#import <UIKit/UIKit.h>
@interface PDAnimatorPUshPopTransition : PDAnimatorBaseTransition
@property (nonatomic, assign)CGPoint itemCenter;
@property (nonatomic, assign)CGSize itemSize;
@property (nonatomic, strong)NSString *imageName;
@end
PDAnimatorPUshPopTransition.m
#import "PDAnimatorPUshPopTransition.h"
@implementation PDAnimatorPUshPopTransition
- (instancetype)init{
self = [super init];
if(self){
}
return self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 5.f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
[super animateTransition:transitionContext];
}
- (void)animationPush{
NSTimeInterval duration = [self transitionDuration:self.transitionContext];
__weak typeof(self) weakSelf = self;
self.containerView.backgroundColor = [UIColor lightGrayColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 160)];
imageView.image = [UIImage imageNamed:self.imageName];
imageView.center = _itemCenter;
CGFloat initialScale = _itemSize.width / CGRectGetWidth(imageView.frame);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, initialScale, initialScale);
transform = CGAffineTransformRotate(transform, M_PI);
imageView.layer.affineTransform = transform;
// imageView.transform = CGAffineTransformMakeScale(initialScale, initialScale);
[self.containerView addSubview:imageView];
self.toView.frame = [self.transitionContext finalFrameForViewController:self.to];
CGPoint finalCenter = self.toView.center;
self.toView.center = finalCenter;
self.toView.alpha = 0.0;
//这一句一定要
[self.containerView addSubview:self.toView];
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.layer.affineTransform = CGAffineTransformIdentity;
imageView.center = finalCenter;
self.fromView.alpha = 0.0;
self.containerView.backgroundColor = [UIColor redColor];
} completion:^(BOOL finished){
[imageView removeFromSuperview];
weakSelf.toView.alpha = 1.0f;
weakSelf.fromView.alpha = 1.0f;
[weakSelf.transitionContext completeTransition:![weakSelf.transitionContext transitionWasCancelled]];
}];
}
- (void)animationPop{
NSTimeInterval duration = [self transitionDuration:self.transitionContext];
__weak typeof(self) weakSelf = self;
self.toView.frame = [self.transitionContext finalFrameForViewController:self.to];
[self.containerView insertSubview:self.toView belowSubview:self.fromView];
self.fromView.alpha = 0.0;
self.fromView.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
CGFloat initialScale = _itemSize.width / 200;
weakSelf.fromView.transform = CGAffineTransformMakeScale(initialScale, initialScale);
weakSelf.fromView.center = weakSelf.itemCenter;
weakSelf.toView.alpha = 1.0f;
} completion:^(BOOL finished){
weakSelf.fromView.alpha = 0.0f;
[weakSelf.transitionContext completeTransition:![weakSelf.transitionContext transitionWasCancelled]];
}];
}
@end










