星空网 > 软件开发 > 操作系统

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

效果图:

iOS转场动画初探images/loading.gif' data-original="http://images2015.cnblogs.com/blog/706432/201512/706432-20151223140503890-727017081.gif" />

补:

//动态添加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转场动画初探

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

北方国际物流园:https://www.goluckyvip.com/tag/102348.html
澳洲国际快递单号查询:https://www.goluckyvip.com/tag/102349.html
购物车放弃率:https://www.goluckyvip.com/tag/10235.html
百世国际物流查询:https://www.goluckyvip.com/tag/102350.html
国际件寄什么快递:https://www.goluckyvip.com/tag/102351.html
大兴机场国际货运站:https://www.goluckyvip.com/tag/102352.html
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流