码迷,mamicode.com
首页 > 移动开发 > 详细

iOS转场动画初探

时间:2015-12-23 15:48:58      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

一般我们就用两种转场push和present

present

/**
 1.设置代理
 - (instancetype)init
 {
 self = [super init];
 if (self) {
 self.transitioningDelegate = self;
 self.modalPresentationStyle = UIModalPresentationCustom;
 }
 return self;
 }
 2.添加协议<UIViewControllerTransitioningDelegate>
 3.重写协议方法 - 在协议方法里面初始化
 - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
 
 }
 
 - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
 
 }
 **/

2.push

第一个控制器推第二个控制器的时候

   ViewController2 *vc = [[ViewController2 alloc]init];
    self.navigationController.delegate = vc; //必须写这句话
    [self.navigationController pushViewController:vc animated:YES];

第二个控制器

@interface ViewController2 : UIViewController<UINavigationControllerDelegate>

@end

代理方法

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {

}

上面就是push和present发生的地方,都需要返回一个 UIViewControllerAnimatedTransitioning

所以我们写一个类

@interface WJTransiation_Mobile : NSObject<UIViewControllerAnimatedTransitioning>

@end

然后修改里面的协议方法

//动画时间

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;

//自定义过渡动画

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

执行过渡动画

1.获取前后控制器

   UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

2.获得转场动画视图

UIView *containerView = [transitionContext containerView];

3.将前后控制器需要进行动画的视图添加到containerView上

   [containerView addSubview:tempView];
    [containerView addSubview:toVC.view];

4.执行自己的动画

demo链接:http://pan.baidu.com/s/1kTPPUMb

效果图:

技术分享

补:

//动态添加transiationView属性

@interface UIViewController (transiationView)

@property (nonatomic,strong)UIView *transiationView;

@end
#import <objc/runtime.h>

@implementation UIViewController (transiationView)

static char transiationViewKey;

- (void)setTransiationView:(UIView *)transiationView {
    return objc_setAssociatedObject(self, &transiationViewKey, transiationView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)transiationView {
     return objc_getAssociatedObject(self, &transiationViewKey);
}

@end

 

iOS转场动画初探

标签:

原文地址:http://www.cnblogs.com/hxwj/p/5069806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!