[IOS 开发] 格瓦拉 控制器之间的换场动画。

2024-05-06 18:58

本文主要是介绍[IOS 开发] 格瓦拉 控制器之间的换场动画。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



先上框架类HYAwesomeTransition
HYAwesomeTransition.h

//
//  HYAwesomTransition.h
//  HYAwesomeTransitionDemo
//
//  Created by nathan on 15/7/30.
//  Copyright (c) 2015年 nathan. All rights reserved.
//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>@interface HYAwesomeTransition : NSObject<UIViewControllerAnimatedTransitioning>@property (nonatomic, assign)CGFloat duration;
@property (nonatomic, assign)CGRect finalFrame;
@property (nonatomic, strong)UIView *containerBackgroundView;
@property (nonatomic, getter=isPresent)BOOL present;- (void)registerStartFrame:(CGRect)startFramefinalFrame:(CGRect)finalFrametransitionView:(UIView *)transitionView;@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

HYAwesomeTransition.m

//
//  HYAwesomTransition.m
//  HYAwesomeTransitionDemo
//
//  Created by nathan on 15/7/30.
//  Copyright (c) 2015年 nathan. All rights reserved.
//#import "HYAwesomeTransition.h"@interface HYAwesomeTransition()@property (nonatomic, strong)UIView *snapshotView;
@property (nonatomic, assign)CGRect startFrame;
@property (nonatomic, copy) void (^completion)(BOOL finished);
@end@implementation HYAwesomeTransition- (void)registerStartFrame:(CGRect)startFramefinalFrame:(CGRect)finalFrametransitionView:(UIView *)transitionView {_startFrame = startFrame;_finalFrame = finalFrame;_snapshotView = [transitionView snapshotViewAfterScreenUpdates:NO];_snapshotView.layer.shadowOpacity = 0.5;_snapshotView.layer.shadowRadius = 3;_snapshotView.layer.shadowColor = [UIColor lightGrayColor].CGColor;_snapshotView.layer.shadowOffset = CGSizeMake(5, 5);
}- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{return self.duration;
}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{if (self.present) {[self presentWithTransiton:transitionContext];}else{[self dismissWithTranstion:transitionContext];}
}- (void)presentWithTransiton:(id <UIViewControllerContextTransitioning>)transitionContext{UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];UIView *containerView = [transitionContext containerView];if (self.containerBackgroundView) {self.containerBackgroundView.frame = containerView.bounds;self.containerBackgroundView.alpha = 0.0;[containerView addSubview:self.containerBackgroundView];}[containerView addSubview:toVC.view];NSTimeInterval duration = [self transitionDuration:transitionContext];CGFloat x = _finalFrame.origin.x - _startFrame.origin.x;CGFloat y = _finalFrame.origin.y - _startFrame.origin.y;UIView *snapshotView = self.snapshotView;snapshotView.frame = _startFrame;[containerView addSubview:snapshotView];CATransform3D upViewTransfrom = CATransform3DIdentity;upViewTransfrom.m34 = 1.0 / -1000;upViewTransfrom = CATransform3DTranslate(upViewTransfrom, 0, 0, 100);CATransform3D middleViewTranfrom = CATransform3DTranslate(upViewTransfrom, x, y, 0);CATransform3D downViewTranfrom   = CATransform3DTranslate(middleViewTranfrom, 0, 0, -100);toVC.view.hidden = YES;NSTimeInterval partDuration = duration / 3;[UIView animateKeyframesWithDuration:partDuration * 3 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{snapshotView.layer.transform = upViewTransfrom;fromVC.view.alpha = 0.0;if (self.containerBackgroundView) {self.containerBackgroundView.alpha = 1.0;}}];[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{snapshotView.layer.transform = middleViewTranfrom;}];} completion:^(BOOL finished) {toVC.view.hidden = NO;CGRect rect = CGRectInset(_finalFrame, -500, -500);CGPathRef startPath = CGPathCreateWithEllipseInRect(rect, NULL);CGPathRef endPath   = CGPathCreateWithEllipseInRect(_finalFrame, NULL);CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];maskLayer.path = startPath;toVC.view.layer.mask = maskLayer;CABasicAnimation *pingAnimation = [CABasicAnimation animationWithKeyPath:@"path"];pingAnimation.fromValue = (__bridge id)(endPath);pingAnimation.toValue   = (__bridge id)(startPath);pingAnimation.duration  = partDuration;pingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];[maskLayer addAnimation:pingAnimation forKey:@"pingInvert"];CGPathRelease(startPath);CGPathRelease(endPath);[UIView animateWithDuration:partDuration animations:^{snapshotView.layer.transform = downViewTranfrom;} completion:^(BOOL finished) {fromVC.view.alpha = 1.0;if (self.containerBackgroundView) {[self.containerBackgroundView removeFromSuperview];}[maskLayer removeFromSuperlayer];[snapshotView removeFromSuperview];[transitionContext completeTransition:YES];}];}];}- (void)dismissWithTranstion:(id <UIViewControllerContextTransitioning>)transitionContext{UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];UIView *containerView = [transitionContext containerView];[containerView addSubview:toVC.view];if (self.containerBackgroundView) {self.containerBackgroundView.frame = containerView.bounds;[containerView addSubview:self.containerBackgroundView];}[containerView addSubview:fromVC.view];CGFloat x = _startFrame.origin.x - _finalFrame.origin.x;CGFloat y = _startFrame.origin.y - _finalFrame.origin.y;UIView *snapshotView = self.snapshotView;snapshotView.transform = CGAffineTransformIdentity;snapshotView.frame = _finalFrame;[containerView addSubview:snapshotView];toVC.view.hidden = YES;CATransform3D upViewTransfrom = CATransform3DIdentity;upViewTransfrom.m34 = 1.0 / -1000;upViewTransfrom = CATransform3DTranslate(upViewTransfrom, 0, 0, 100);CATransform3D middleViewTranfrom = CATransform3DTranslate(upViewTransfrom, x, y, 0);CATransform3D downViewTranfrom   = CATransform3DTranslate(middleViewTranfrom, 0, 0, -100);NSTimeInterval partDuration = [self transitionDuration:transitionContext] / 3;CGRect rect = CGRectInset(_finalFrame, -500, -500);CGPathRef endPath   = CGPathCreateWithEllipseInRect(rect, NULL);CGPathRef startPath = CGPathCreateWithEllipseInRect(_finalFrame, NULL);CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];maskLayer.path = startPath;fromVC.view.layer.mask = maskLayer;CABasicAnimation *pingAnimation = [CABasicAnimation animationWithKeyPath:@"path"];pingAnimation.fromValue = (__bridge id)(endPath);pingAnimation.toValue   = (__bridge id)(startPath);pingAnimation.duration  = partDuration;pingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];[maskLayer addAnimation:pingAnimation forKey:@"pingInvert"];CGPathRelease(startPath);CGPathRelease(endPath);[UIView animateWithDuration:partDuration animations:^{snapshotView.layer.transform = upViewTransfrom;} completion:^(BOOL finished) {[fromVC.view removeFromSuperview];toVC.view.hidden = NO;[UIView animateKeyframesWithDuration:partDuration * 2 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{snapshotView.layer.transform = middleViewTranfrom;}];[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{snapshotView.layer.transform = downViewTranfrom;if (self.containerBackgroundView) {self.containerBackgroundView.alpha = 0.0;}}];} completion:^(BOOL finished) {if (self.containerBackgroundView) {[self.containerBackgroundView removeFromSuperview];}[snapshotView removeFromSuperview];[transitionContext completeTransition:YES];}];}];
}@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

Example

self.awesometransition = [[HYAwesomeTransition alloc] init];
self.awesometransition.duration = 2.0f;
self.awesometransition.containerBackgroundView = customView;
[self.awesometransition registerStartFrame:startFramefinalFrame:finalFrametransitionView:cell];[self presentViewController:vc animated:YES completion:^{vc.avatar.hidden = NO;
}];

Implement UIViewControllerTransitioningDelegate and this delegate method:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{self.awesometransition.present = YES;return self.awesometransition;
}- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{self.awesometransition.present = NO;return self.awesometransition;
}

If you use UINavigationController,you have to implement UINavigationControllerDelegate instead of UIViewControllerTransitioningDelegate, but interactive gesture is not suppored yet.
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationControlleranimationControllerForOperation:(UINavigationControllerOperation)operationfromViewController:(UIViewController *)fromVCtoViewController:(UIViewController *)toVC{if (operation != UINavigationControllerOperationPush) {self.awesometransition.present = NO;}else{self.awesometransition.present = YES;}return self.awesometransition;
}



这篇关于[IOS 开发] 格瓦拉 控制器之间的换场动画。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965101

相关文章

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA