iOS转场动画初探

一般我们就用两种转场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
时间: 2024-08-28 07:14:25

iOS转场动画初探的相关文章

iOS 转场动画

Inherits from CAAnimation : NSObject Conforms to NSCoding (CAAnimation)NSCopying (CAAnimation)CAAction (CAAnimation)CAMediaTiming (CAAnimation)NSObject (NSObject) Framework /System/Library/Frameworks/QuartzCore.framework Availability Available in iOS

iOS:核心动画之转场动画CATransition

转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 动画属性: –type:动画过渡类型 –subtype:动画过渡方向 –startProgress:动画起点(在整体动画的百分比) –endProgress:动画终点(在整体动画的百分比

iOS开发UI篇—核心动画(转场动画和组动画)

iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比)

iOS自定义转场动画实战讲解

iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的

IOS开发核心动画篇—转场动画和组动画

iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比)

iOS开发——动画编程OC篇&amp;(四)转场动画

转场 动画 一.转场动画简单介绍 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比) 二.转场动画代码示例 1.界面搭建

iOS 7 present/dismiss转场动画

前言 iOS 7以后提供了自定义转场动画的功能,我们可以通过遵守协议完成自定义转场动画.本篇文章讲解如何实现自定义present.dismiss自定义动画. 效果图 本篇文章实现的动画切换效果图如下: 视图切换种类 如下效果图,这是有两大类视图切换动画的,一种是交互式的,另一种就是自定义的. 本篇只讲其中的UIViewControllerAnimatedTransitioning协议,来实现present.dismiss动画效果.另外的几个,后面会继续学习总结!!! 协议 我们要实现presen

iOS 开发--转场动画

"用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同一个导航控制器你可以自定义转场动画实现两个viewController之间的过渡.实际上在iOS7之后,通过实现 UIViewControllerAnimatedTransitioning或者UIViewControllerContextTransitioning协议, 就可以简单的自定义转场动画,比如一个N

iOS自定义转场动画

图1是最近闲着做的一个项目:午睡闹钟(欢迎到AppStore下载,截稿时最新版还在审核)的截图,把其中的转场动画效果简单封装了一下写了个demo(图2),demo的备注写的比较清楚,demo可以在github下载:转到github ,因为明天还要交作业,有空再详细写实现原理吧,可以参考下面这两篇文章,写的比较详细的: (1)http://onevcat.com/2013/10/vc-transition-in-ios7/ (2)http://www.cocoachina.com/ios/2013