[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

相关文章

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1