UIView animateWithDuration:不在UIStoryboardSegue执行

时间:2014-12-02 15:09:16

标签: ios objective-c animation uiview

我正在编写一个自定义UIStoryboardSegue,它会在perform方法的两个视图控制器(源和目标)中触发动画。以下是:

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

    //
    UIView * sourceView = sourceViewController.view;
    UIView * destinationView = destinationViewController.view;

    [sourceView addSubview:destinationView];

    id <HyTransition> presentingTransition = [self presentingTransitionForViewController:destinationViewController];
    id <HyTransition> dismissingTransition = [self dismissingTransitionForViewController:sourceViewController];

    [presentingTransition performWithTransitionContext:self.presentingContext];
    [dismissingTransition performWithTransitionContext:self.dismissingContext];
}

每个视图控制器都返回一个id <HyTransition>类型的对象,该对象负责制作动画。在这种情况下,我返回HyTransitionWithCallback,这是一个子类。然后我动画一些约束:

- (id <HyTransition>)presentingTransition
{
    __weak HyPresentationViewController * weakSelf = self;

    return [HyTransitionWithCallback transitionWithCallback:^(__weak HyTransitionContext * context) {

        [weakSelf.pageControlBottomConstraint setConstant:-weakSelf.view.frame.size.height];
        [weakSelf.loginButtonBottomConstraint setConstant:-weakSelf.view.frame.size.height];
        [weakSelf.view layoutIfNeeded];

        [UIView animateWithDuration:0.4f
                              delay:1.0f
             usingSpringWithDamping:1.0f
              initialSpringVelocity:0.0f
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             [weakSelf.pageControlBottomConstraint setConstant:56.0f];
                             [weakSelf.loginButtonBottomConstraint setConstant:8.0f];
                             [weakSelf.view layoutIfNeeded];
                         }
                         completion:^ (BOOL finished) {

                             if (finished) {
                                 [context setTransitionFinished:YES];
                             }
                         }];
    }];
}

正在调用此方法,但动画根本没有运行。我甚至尝试将延迟设置为10.0f,但立即调用completion块(finished设置为YES),当视图显示时,约束位于其最终位置(56.0f8.0f)。

我的第一个猜测是动画过早触发,但我不能在perform方法中为目标视图设置动画效果吗?似乎这个方法在viewWillAppear之前被调用,这就是为什么我说它可能过早被调用。

编辑:在动画之前,viewWillAppear:不仅被称为,而且viewDidAppear:也是如此。我想这个动画毕竟不会被触发,但这也意味着我不知道动画为什么不运行。

编辑:好的,所以我让动画工作,但不是我想要的那个。此代码有效:

- (id <HyTransition>)presentingTransition
{
    __weak HyPresentationViewController * weakSelf = self;

    return [HyTransitionWithCallback transitionWithCallback:^(__weak HyTransitionContext * context) {

        [weakSelf.pageControlBottomConstraint setConstant:100.0f];
        [weakSelf.loginButtonBottomConstraint setConstant:100.0f];
        [weakSelf.view setNeedsUpdateConstraints];

        [UIView animateWithDuration:5.4f
                         animations:^ {
                             [weakSelf.view layoutIfNeeded];
                         }
                         completion:^ (BOOL finished) {

                             if (finished) {
                                 [context setTransitionFinished:YES];
                             }
                         }];
    }];
}

这激发了他们自然位置的约束,但我想要完全相反。也就是说,首先我想错放约束,然后将它们动画到它们的位置。这就是我所拥有的:

- (id <HyTransition>)presentingTransition
{
    __weak HyPresentationViewController * weakSelf = self;

    return [HyTransitionWithCallback transitionWithCallback:^(__weak HyTransitionContext * context) {

        [weakSelf.pageControlBottomConstraint setConstant:100.0f];
        [weakSelf.loginButtonBottomConstraint setConstant:100.0f];
        [weakSelf.view setNeedsUpdateConstraints];
        [weakSelf.view layoutIfNeeded];

        [weakSelf.pageControlBottomConstraint setConstant:56.0f];
        [weakSelf.loginButtonBottomConstraint setConstant:8.0f];
        [weakSelf.view setNeedsUpdateConstraints];

        [UIView animateWithDuration:5.4f
                         animations:^ {
                             [weakSelf.view layoutIfNeeded];
                         }
                         completion:^ (BOOL finished) {

                             if (finished) {
                                 [context setTransitionFinished:YES];
                             }
                         }];
    }];
}

这不起作用;它没有再动画。

编辑:这很奇怪......请使用以下代码:

- (id <HyTransition>)presentingTransition
{
    __weak HyPresentationViewController * weakSelf = self;

    return [HyTransitionWithCallback transitionWithCallback:^(__weak HyTransitionContext * context) {

        [weakSelf.pageControlBottomConstraint setConstant:100.0f];
        [weakSelf.loginButtonBottomConstraint setConstant:100.0f];
        [weakSelf.view setNeedsUpdateConstraints];
        [weakSelf.view updateConstraints];

        [weakSelf.pageControlBottomConstraint setConstant:56.0f];
        [weakSelf.loginButtonBottomConstraint setConstant:8.0f];
        [weakSelf.view setNeedsUpdateConstraints];

        [UIView animateWithDuration:5.4f
                         animations:^ {
                             [weakSelf.view layoutIfNeeded];
                         }
                         completion:^ (BOOL finished) {

                             if (finished) {
                                 [context setTransitionFinished:YES];
                             }
                         }];
    }];
}

动画运行,但根本不是我所期待的。页面控件什么都不做,按钮动画字体大小!按钮的文字从非常小到标准尺寸...这必须是伏都教xD

0 个答案:

没有答案