gpt4 book ai didi

ios - 如何使用 animateWithDuration : when presenting view controller on iOS 7?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:04 24 4
gpt4 key购买 nike

我目前正在使用以下代码来展示我的 View Controller 。

CATransition* transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromBottom;
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:viewController animated:NO completion:nil];

我需要使用 UIView animateWithDuration 方法来使用更复杂的动画。它只是模态呈现,背景 View Controller 应该留在那里。如何动画呈现 View Controller 的 View ?

更新:下一个版本的代码:)

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:NULL];
RootViewControllerEx *viewController = (RootViewControllerEx *)[sb instantiateViewControllerWithIdentifier:@"SB_RDVC"];
viewController.transitioningDelegate = self;
viewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:viewController animated:YES completion:nil];
...

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
...
}

Storyboard中的 VC 没有指定任何 segue,它在 Storyboard中保持孤独 :)

最佳答案

您需要使用 UIModalPresentationCustom 作为您的 UIViewController modalPresentationStyle

然后您需要创建一个符合 UIViewControllerTransitioningDelegate 的类,并在 presentedViewController 上设置它。

例子

YourViewController *viewController = [[YourViewController alloc] init];
viewController.delegate = self;
viewController.transitioningDelegate = self;
viewController.modalPresentationStyle = UIModalPresentationCustom;

[self.navigationController presentViewController:viewController animated:YES completion:nil];

然后您必须实现以下方法:

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;

它返回一个符合 UIViewControllerAnimatedTransitioning 协议(protocol)的对象,该协议(protocol)实现了以下方法:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIView *inView = [transitionContext containerView];
UIView *toView = [[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey] view];

toView.frame = //CGRectMake(0,0,0,0);
[inView addSubview:toView];
// Use whatever animateWithDuration you need
[UIView animateWithDuration:0.6f delay:0.0f usingSpringWithDamping:0.7f initialSpringVelocity:0.5f options:UIViewAnimationOptionCurveEaseIn animations:^{
// Custom animation here
} completion:^(BOOL finished) {
// IMPORTANT
[transitionContext completeTransition:YES];
}];
}

始终记得在转换完成时告知上下文,否则您将进入不稳定状态。

关于ios - 如何使用 animateWithDuration : when presenting view controller on iOS 7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937262/

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