直接上代码!
1、准备工作:
默认的ViewController类,和创建要present到的下一级控制器SecondViewController类。默认创建,不添加其他操作,可以自己改改背景色做区分。
2、实现present动画:
这里演示的动画类似原生的push和pop动画,present时界面由右向左覆盖上一级界面,dismiss相反过程。
2.1、创建一个继承自NSObject的类LXFLeftPresentAnimation,遵守UIViewControllerAnimatedTransitioning协议
// LXFLeftPresentAnimation.h
#import <UIKit/UIKit.h>
@interface LXFLeftPresentAnimation : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) BOOL isPresent;
@end
2.2、实现UIViewControllerAnimatedTransitioning协议中的方法
// LXFLeftPresentAnimation.m
#import "LXFLeftPresentAnimation.h"
@implementation LXFLeftPresentAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 1.f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *toView = nil;
UIView *fromView = nil;
UIView *transView = nil;
if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
toView &