gpt4 book ai didi

iOS - 创建自定义推送转场动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:50 27 4
gpt4 key购买 nike

我正在尝试创建一个推送转场动画,其中 sourceVC 垂直下降离开屏幕,而 destinationVC 垂直向上出现在屏幕上。我的自定义 segue 似乎几乎制作了这个动画,但是,在出现 destinationVC 时动画不起作用。这是我的 -perform 转场。

-(void)perform
{
UIViewController *sourceVC = self.sourceViewController;
UIViewController *destVC = self.destinationViewController;
[sourceVC.view addSubview:destVC.view];

float width = sourceVC.view.frame.size.width;
float height = sourceVC.view.frame.size.height;

destVC.view.frame = CGRectMake(0, 560, width, height);

[UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
[destVC.view removeFromSuperview];
sourceVC.view.frame = CGRectMake(0, 560, width, height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
destVC.view.frame = CGRectMake(0, 0, destVC.view.frame.size.width, destVC.view.frame.size.height);
} completion:^(BOOL finished) {
[sourceVC.navigationController pushViewController:destVC animated:NO];
}];
}];
}

第二个动画是无效的。

知道我的代码有什么问题吗?谢谢你的帮助。

最佳答案

我需要创建一个符合 UIViewControllerAnimatedTransitioning 的自定义 Animator 类。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface SAAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end

然后我实现了所需的方法。

@implementation SAAnimator

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.2;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
fromViewController.view.userInteractionEnabled = NO;

[[transitionContext containerView]addSubview:fromViewController.view];
[[transitionContext containerView]addSubview:toViewController.view];

toViewController.view.frame = CGRectMake(0, 560, toViewController.view.frame.size.width, toViewController.view.frame.size.height);

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
fromViewController.view.frame = CGRectMake(0, 560, fromViewController.view.frame.size.width, fromViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
toViewController.view.frame = CGRectMake(0, 0, toViewController.view.frame.size.width, toViewController.view.frame.size.height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}];
}
@end

然后非常简单,在执行推送转场的 View Controller 中,我使类符合 UINavigationControllerDelegate 并实现此方法。

#pragma mark - Animated Transiton

-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPush) {
SAAnimator * animator = [SAAnimator new];
return animator;
}
return nil;
}

在 prepareForSegue 中设置导航 Controller 的委托(delegate)

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"toCreateVC"]) {
self.navigationController.delegate = self;
}
}

关于iOS - 创建自定义推送转场动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29614623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com