自定义转场动画,在iOS7及以上的版本才开始出现的,在一些应用中,我们常常需要定制自定义的的跳转动画

1.遵守协议:<UIViewControllerAnimatedTransitioning>

2.协议的方法主要的是两个:

// 指定动画的持续时长 1. (NSTimeInterval)transitionDuration; 
// 转场动画的具体内容 2. (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> typedef NS_ENUM(NSUInteger, HYBControllerTransitionType) {
kControllerTransitionPush = << ,
kControllerTransitionPop = <<
}; @interface HYBControllerTransition : NSObject <UIViewControllerAnimatedTransitioning> + (instancetype)transitionWithType:(HYBControllerTransitionType)transitionType
duration:(NSTimeInterval)duration; @end
#import "HYBControllerTransition.h"
#import "ViewController.h"
#import "DetailController.h" @interface HYBControllerTransition () @property (nonatomic, assign) HYBControllerTransitionType transitionType;
@property (nonatomic, assign) NSTimeInterval duration; @end @implementation HYBControllerTransition - (instancetype)init {
if (self = [super init]) {
self.transitionType = kControllerTransitionPush;
} return self;
} + (instancetype)transitionWithType:(HYBControllerTransitionType)transitionType
duration:(NSTimeInterval)duration {
HYBControllerTransition *transition = [[HYBControllerTransition alloc] init];
transition.transitionType = transitionType;
transition.duration = duration; return transition;
} #pragma mark - UIViewControllerAnimatedTransitioning
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
switch (self.transitionType) {
case kControllerTransitionPush: {
[self push:transitionContext];
break;
}
case kControllerTransitionPop: {
[self pop:transitionContext];
break;
}
default: {
break;
}
}
} - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return self.duration;
} - (void)animationEnded:(BOOL)transitionCompleted {
NSLog(@"%s", __FUNCTION__);
} #pragma mark - Private
- (void)pop:(id<UIViewControllerContextTransitioning>)transitionContext {
DetailController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
ViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView]; UIView *toImageView = toVC.isImg1 ? toVC.img1 : toVC.img2; UIView *tempView = containerView.subviews.lastObject; // 第一个view是fromVC.view
// 第二个view是push进来时所生成的toImageView截图
for (UIView *view in containerView.subviews) {
NSLog(@"%@", view);
if (fromVC.view == view) {
NSLog(@"YES");
}
} toImageView.hidden = YES;
tempView.hidden = NO;
// 必须保证将toVC.view放在最上面,也就是第一个位置
[containerView insertSubview:toVC.view atIndex:]; [UIView animateWithDuration:self.duration
delay:0.0
usingSpringWithDamping:0.55
initialSpringVelocity:/ 0.55
options:
animations:^{
fromVC.view.alpha = 0.0;
tempView.frame = [toImageView convertRect:toImageView.bounds toView:containerView];
} completion:^(BOOL finished) {
tempView.hidden = NO;
toImageView.hidden = NO;
[tempView removeFromSuperview]; [transitionContext completeTransition:YES]; for (UIView *view in containerView.subviews) {
NSLog(@"%@", view);
if (toVC.view == view) {
NSLog(@"YES");
}
}
}];
} - (void)push:(id<UIViewControllerContextTransitioning>)transitionContext {
ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
DetailController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView]; UIView *fromImageView = fromVC.isImg1 ? fromVC.img1 : fromVC.img2;
UIView *tempView = [fromImageView snapshotViewAfterScreenUpdates:NO];
tempView.frame = [fromImageView convertRect:fromImageView.bounds toView:containerView]; UIView *toImageView = toVC.imgView; fromImageView.hidden = YES;
toVC.view.alpha = 0.0;
toImageView.hidden = YES; [containerView addSubview:toVC.view];
[containerView addSubview:tempView]; [UIView animateWithDuration:self.duration
delay:0.0
usingSpringWithDamping:0.55
initialSpringVelocity:/ 0.55
options:
animations:^{
toVC.view.alpha = 1.0;
tempView.frame = [toImageView convertRect:toImageView.bounds toView:containerView];
} completion:^(BOOL finished) {
tempView.hidden = YES;
toImageView.hidden = NO; [transitionContext completeTransition:YES];
}];
}
@end

使用遵守Nav协议的方法:

#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
return nil;
} - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
if (operation == UINavigationControllerOperationPush) {
return [HYBControllerTransition transitionWithType:kControllerTransitionPush duration:0.75];
} else {
return [HYBControllerTransition transitionWithType:kControllerTransitionPop duration:0.75];
}
}

第六十五篇、OC_iOS7 自定义转场动画push pop的更多相关文章

  1. 第六十五篇:Vue的过滤器

    好家伙, 过滤器,vue3取消了,只有vue2能用 1.过滤器 过滤器(Filters)是vue为开发者提供的功能,常用于文本的格式化. 过滤器可以用在两个地方:插值表达式和v-bind属性绑定. 过 ...

  2. 《手把手教你》系列技巧篇(六十五)-java+ selenium自动化测试 - cookie -下篇(详细教程)

    1.简介 今天这一篇,宏哥主要讲解:利用WebDriver 提供可以读取.添加和删除cookie 信息的相关操作方法.验证浏览器中是否存在某个cookie.原因是:因为基于真实的cookie 的测试是 ...

  3. Python之路【第十五篇】:Web框架

    Python之路[第十五篇]:Web框架   Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 1 2 3 4 5 6 ...

  4. Gradle 1.12用户指南翻译——第六十五章. Maven 发布(新)

    其他章节的翻译请参见:http://blog.csdn.net/column/details/gradle-translation.html翻译项目请关注Github上的地址:https://gith ...

  5. 孤荷凌寒自学python第六十五天学习mongoDB的基本操作并进行简单封装4

    孤荷凌寒自学python第六十五天学习mongoDB的基本操作并进行简单封装4 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十一天. 今天继续学习mongoDB的简单操作 ...

  6. FreeSql (三十五)CodeFirst 自定义特性

    比如项目内已经使用了其它 orm,如 efcore,这样意味着实体中可能存在 [Key],但它与 FreeSql [Column(IsPrimary = true] 不同. Q: FreeSql 实体 ...

  7. Egret入门学习日记 --- 第十五篇(书中 6.1~6.9节 内容)

    第十五篇(书中 6.1~6.9节 内容) 好的,昨天完成了第五章. 今天来看第六章. 总结重点: 1.如何对组件进行分组? 跟着做: 重点1:如何对组件进行分组? 首先,选中你想要组合的组件. 然后点 ...

  8. OpenCV开发笔记(六十五):红胖子8分钟带你深入了解ORB特征点(图文并茂+浅显易懂+程序源码)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  9. 解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)

    解剖SQLSERVER 第十五篇  SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-sourc ...

随机推荐

  1. mount nfs的可选参数

    mount nfs的可选参数:HARD mount和SOFT MOUNT:HARD:NFS CLIENT会不断的尝试与SERVER的连接(在后台,不会给出任何提示信息,在LINUX下有的版本仍然会给出 ...

  2. iOS有关截图的操作

    1.截取选中view的图片 //根据size大小创建一个基于位图的图形上下文 CGRect rect =view.frame; UIGraphicsBeginImageContext(rect.siz ...

  3. PI-webservice05-SAP调用外部webservice

    在用webservice进行数据传输的过程中,SAP系统与.net开发的信息系统之间的数据调用.如何用SAP调用外部的.net系统发布的webservice程序来获取外部的数据,详情请见下文: 1,创 ...

  4. Codeforces Round #332 (Div. 2) A. Patrick and Shopping 水题

    A. Patrick and Shopping Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  5. lightOJ 1030(期望)

    题意:有一个迷宫是1×n的格子,一个人每到一个格子就能够把这个格子内的金子所有拿走,刚開始站在第1个格子,然后開始掷骰子得到点数x,他就要从当前位置走到加x的位置.假设发现位置是大于n的就又一次掷骰子 ...

  6. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  7. Winform 水印TextBox

    方法一: public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label() ...

  8. android138 360 小火箭

    package com.itheima52.rocket; import android.app.Service; import android.content.Context; import and ...

  9. c#线程--生产者和消费者

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  10. 新浪云(SAE)使用没有内置的django版本

    SAE自带的django目前到1.5版本,如果要使用更高的版本,则需要把django包同代码一起上传. 以1.7.3为例 先从SAE svn签出代码,默认1是根目录 1.  1目录下创建文件夹 sit ...