gpt4 book ai didi

ios - 使用自定义 segue 转到 ViewController 会导致 viewWillAppear 被调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:59:28 25 4
gpt4 key购买 nike

我写了一个自定义转场,因为我想给它添加我自己的动画。除了目标 View CiewController 中的 viewDidLoad 方法被调用两次之外,一切正常。这是我的 segue 的执行方法:

- (void)perform
{
UIViewController* sourceViewController = self.sourceViewController;
UIViewController* destinationViewController = self.destinationViewController;

[sourceViewController.view addSubview:destinationViewController.view];

CGPoint originalCenter = destinationViewController.view.center;
destinationViewController.view.center = CGPointMake(self.originatingPoint.x * 3, self.originatingPoint.y);

[UIView animateWithDuration:0.25
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
destinationViewController.view.center = originalCenter;
}
completion:^(BOOL finished) {
[sourceViewController presentViewController:destinationViewController animated:NO completion:NULL]; // present VC
}];
}

有人知道是什么原因造成的吗?

[----- 编辑 -----]

segue 作为自定义 segue 出现在故事​​板中,带有我自己编写的 segue 类。在我的类(class)中唯一不同的是我在上面提出的执行方法。通过按钮调用segue,只调用一次prepareForSegue方法。

[----- 编辑 2 -----]

我检查了 targetVC 的 viewDidLoad 方法,每个 segue 只调用一次。尽管如此,使用 viewWillAppear 对我来说会方便得多,所以您是否知道我制作此动画的不同方式?

最佳答案

我建议您使用 UIViewControllerTransitioningDelegateUIViewControllerAnimatedTransitioning,它们是自 iOS 7.0 以来更适合用于转换的约定。从 iOS 8.0 开始,您还获得了 UIPresentationController 支持,这使您可以构建更丰富的过渡效果。

例子:

@interface ModalTransitionAnimator : NSObject<UIViewControllerAnimatedTransitioning>

@property (nonatomic) CGPoint originatingPoint;

@end

@implementation ModalTransitionAnimator

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIView* sourceView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView* destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
UIView* container = transitionContext.containerView;

[container addSubview:destinationView];

CGPoint originalCenter = destinationView.center;

destinationView.center = CGPointMake(self.originatingPoint.x * 3, self.originatingPoint.y);

[UIView animateWithDuration:duration animations:^{
destinationView.center = originalCenter;
}
completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}

@end

然后在 prepareForSegue 中,您只需分配转换委托(delegate)并实现 UIViewControllerTransitioningDelegate 以返回适当的动画师以进行演示或解雇。

@interface ViewController : UIViewController<UIViewControllerTransitioningDelegate>
@end

@implementation ViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIViewController* controller = (UIViewController*)segue.destinationViewController;

controller.transitioningDelegate = self;
controller.modalPresentationStyle = UIModalPresentationCustom;
controller.modalPresentationCapturesStatusBarAppearance = YES;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
ModalTransitionAnimator *animator = [[ModalTransitionAnimator alloc] init];
animator.originatingPoint = /* ... */;

return animator;
}

@end

因为这是一个模态转换,你必须使用 presentViewController:animated: 来呈现 Controller 。因此,在 Storyboards 中使用正常的“显示”segues,它们将自动运行引擎盖下的所有动画,无需在这里重新发明 segues。

我在 Github 的某处有关于如何构建自定义转换的示例:

https://github.com/pronebird/CustomModalTransition/tree/ios8

关于ios - 使用自定义 segue 转到 ViewController 会导致 viewWillAppear 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253112/

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