【Swift学习笔记-《PRODUCT》读书记录-实现自定义转场动画】

iOS默认的push动画是把即将展示的控制器从右边推过来。有时我们想实现类似PPT中的一些动画,这时候就需要自定义转场动画了。如下图我们想实现一个淡出并且放大的过场动画,在退出时是一个淡出缩小的动画。

首先需要自定义一个类DiaryAnimator.swift遵守 UIViewControllerAnimatedTransitioning 协议,然后实现它,代码如下:

import UIKitclass DiaryAnimator: NSObject, UIViewControllerAnimatedTransitioning {// 用于接受外界的operationvar operation:UINavigationControllerOperation!// 返回动画时间func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {return 0.4}// 要设置的动画//UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations.func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {let containerview = transitionContext.containerViewlet fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)let fromView = fromVC!.viewlet toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)let toView = toVC?.view// 设置新场景透明度toView?.alpha = 0.0// 设置初始值if operation == UINavigationControllerOperation.pop {fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)}else{toView?.transform = CGAffineTransform(scaleX: 0.3, y: 0.3)}containerview.insertSubview(toView!, aboveSubview: fromView!)UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {if self.operation ==  UINavigationControllerOperation.pop {// 放大要转出的场景fromView?.transform = CGAffineTransform(scaleX: 3.3,y: 3.3)} else {// 设置新场景为原始大小toView?.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)}toView?.alpha = 1.0},completion: { finished intransitionContext.completeTransition(true)})}}

 自定义好动画后,在要使用的控制器中实现 UINavigationControllerDelegate 协议的 optional func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 方法,返回自定义的动画即可

extension HomeYearController : UINavigationControllerDelegate
{func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {let animate = DiaryAnimator()animate.operation = operationreturn animate}
}

 



转载于:https://www.cnblogs.com/heyode/p/6513581.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269878.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【JZOJ3598】【CQOI2014】数三角形

Mission 对于100%的数据1<m,n<1000 Solution 鬼题&#xff0c;ansC3(n∗m)−Ans&#xff0c;其中Ans表示三点共线的数目&#xff1b; 枚举最长边的向量(x,y)&#xff0c;容易算出贡献及个数。 Code #include<iostream> #include<stdio.h> #include<algor…

NSTimer定时器进阶——详细介绍,循环引用分析与解决

引言 定时器&#xff1a;A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. 翻译如下&#xff1a;在固定的时间间隔被触发&#xff0c;然后给指定目标发送消息。总结为三要素吧&#xff1a;时间间隔、…

35:字符串的展开

35:字符串的展开 查看提交统计提问总时间限制: 1000ms内存限制: 65536kB描述在初赛普及组的“阅读程序写结果”的问题中&#xff0c;我们曾给出一个字符串展开的例子&#xff1a;如果在输入的字符串中&#xff0c;含有类似于“d-h”或者“4-8”的字串&#xff0c;我们就把它当作…

Josn

1转载于:https://www.cnblogs.com/zouxiaofan/p/6523998.html

HTML - 超文本标记语言 (Hyper Text Markup Language)

HTML - 超文本标记语言 (Hyper Text Markup Language) HTML是建设网站/网页制作主要语言。 HTML是一种易于学习的标记语言。 HTML使用像 <p> 尖括号内标记标签来定义网页的内容&#xff1a; HTML 实例 <html><body><h1>My First Heading</h1><…

AOP切入同类调用方法不起作用,AopContext.currentProxy()帮你解决这个坑

原来在springAOP的用法中&#xff0c;只有代理的类才会被切入&#xff0c;我们在controller层调用service的方法的时候&#xff0c;是可以被切入的&#xff0c;但是如果我们在service层 A方法中&#xff0c;调用B方法&#xff0c;切点切的是B方法&#xff0c;那么这时候是不会切…

[转]Installing Memcached on Windows

Installing Memcached on Windows 原文链接https://commaster.net/content/installing-memcached-windowsSubmitted by COMMASTER21JAN 15Memcached is a high performance, in-memory key-value store or caching system. Its main purpose is to speed up web applications b…

Java高级面试题

Java多线程 1、线程池的原理&#xff0c;为什么要创建线程池&#xff1f; 答&#xff1a;1)线程池可以降低创建和销毁线程时的资源消耗&#xff0c;提高响应速度&#xff0c;提高现成的可管理性。 2)线程池构造参数&#xff1a; corePoolSize:核心线程数 maximumPoolSize:最大…

访问修改属性日志

1 import time as t2 3 class Record:4 def __init__(self,value None,name None):5 self.value value6 self.name name7 8 def __get__(self,instance,owner):9 with open(D://record.txt,a) as f: 10 f.write(%s变量于北京时…

不自定义异步方法的线程池默认使用SimpleAsyncTaskExecutor

如果不自定义异步方法的线程池默认使用SimpleAsyncTaskExecutor。SimpleAsyncTaskExecutor&#xff1a;不是真的线程池&#xff0c;这个类不重用线程&#xff0c;每次调用都会创建一个新的线程。并发大的时候会产生严重的性能问题。 定义通用线程池 EnableAsync Configuratio…

同步Android与PC的时间

同步Android与PC的时间 在做一些网络延迟测试的时候&#xff0c;需要同步Android设备或者模拟器与PC的时间&#xff08;要不然无法准确计算延迟&#xff09;&#xff0c;在这里记一下获取Android的时间戳以及MacOS的时间戳&#xff0c;均为纳秒级 Android: adb shell echo \$EP…

AopContext.currentProxy();为什么能获取到代理对象

在同一个类中&#xff0c;非事务方法A调用事务方法B&#xff0c;事务失效&#xff0c;得采用AopContext.currentProxy().xx()来进行调用&#xff0c;事务才能生效。 B方法被A调用&#xff0c;对B方法的切入失效&#xff0c;但加上AopContext.currentProxy()创建了代理类&#x…

java中 set,list,array(集合与数组)相互转换

1 public static Object[] List2Array(List<Object> oList) { 2 Object[] oArray oList.toArray(new Object[] {}); 3 // TODO 需要在用到的时候另外写方法&#xff0c;不支持泛型的Array. 4 return oArray; 5 } 6 7 publi…

@Async注解导致循环依赖,BeanCurrentlyInCreationException异常

使用Async异步注解导致该Bean在循环依赖时启动报BeanCurrentlyInCreationException异常的根本原因分析&#xff0c;以及提供解决方案 今天在自己项目中使用Async的时候&#xff0c;碰到了一个问题&#xff1a;Spring循环依赖&#xff08;circular reference&#xff09;问题。 …

人工智能:图像数字化相关的知识介绍

❤️作者主页&#xff1a;IT技术分享社区 ❤️作者简介&#xff1a;大家好,我是IT技术分享社区的博主&#xff0c;从事C#、Java开发九年&#xff0c;对数据库、C#、Java、前端、运维、电脑技巧等经验丰富。 ❤️个人荣誉&#xff1a; 数据库领域优质创作者&#x1f3c6;&#x…