交互式过渡动画未完成

时间:2014-04-24 14:50:23

标签: objective-c ios7 core-animation uiviewanimation

当我使用交互式委托调用动画时,我的视图控制器上有自定义动画过渡,动画工作正常。当我添加正常工作的交互式组件时。但是,当我在使用交互式组件之前使用按钮来解除像我之前那样,动画完成块没有被调用。

下面是动画块。起初我使用动画块的延迟/选项变化并将其更改为更简单的动画/完成以确保它不是该功能。如果我从动画块中删除所有代码,它将执行并关闭控制器(尽管没有动画)。我不知道什么是冷错,也找不到类似的问题。

手势只是向右滑动手指以解除模态......这很好用。当我删除交互式部分时,按下按钮时动画运行正常。仅当我将交互式组件附加到View Controller时才会发生这种情况。

这是动画块:

[UIView animateWithDuration:PRESENT_DURATION animations:^{
            CGAffineTransform newTransform = fromViewController.view.transform;
            newTransform = CGAffineTransformTranslate(transform, adjustedBounds.size.width, height);
            fromViewController.view.transform = newTransform;
            toViewController.view.alpha = 1;
        } completion:^(BOOL finished){
            NSLog(@"Finished Animation");
            if ([transitionContext transitionWasCancelled]) {
                [transitionContext completeTransition:NO];
            } else {
                [fromViewController.view removeFromSuperview];
                [transitionContext completeTransition:YES];
            }
        }];

这是我将animate和interactionController附加到ViewController

的地方
- (IBAction)settingsButtonClicked:(id)sender
{
    UINavigationController *navigationController =[[self storyboard] instantiateViewControllerWithIdentifier:@"SettingsNavigationViewController"];
    navigationController.modalPresentationStyle = UIModalPresentationCustom;

    navigationController.transitioningDelegate = self;
    navigationController.edgesForExtendedLayout = UIRectEdgeNone;
    [self presentViewController:navigationController animated:YES completion:^{
        id animator = [navigationController.transitioningDelegate animationControllerForDismissedController:self];
        id interactor = [navigationController.transitioningDelegate interactionControllerForDismissal:animator];

        if ([interactor respondsToSelector:@selector(attachToViewController:)]) {
            [interactor attachToViewController:self];
        }
    }];
    navigationController.view.superview.frame = CGRectMake(0, 0, navigationController.view.frame.size.width, self.view.frame.size.height);
}

1 个答案:

答案 0 :(得分:1)

我在使用交互式百分比驱动转换时遇到了同样的问题,因为平移驱动手势会很快发生并且在启动后会立即取消。我相信在演示之后过早地调用完成/取消会在Apple代码中显示某个错误,该错误会忘记调用正在进行的动画的完成块。

最后通过覆盖cancelInteractiveTransition来更新它,使用0%更新它:

override func cancelInteractiveTransition() {
    updateInteractiveTransition(0)
    super.cancelInteractiveTransition()
}

在obj C中

-(void)cancelInteractiveTransition {
    [self updateInteractiveTransition:0];
    [super cancelInteractiveTransition];
}

希望这能让你接近答案,我会在finishIneractiveTransition中做类似的事情,也许尝试在下一个运行循环中调用super来调用

编辑:你仍然可以通过以很快的速度来回滑动来完成动画,所以我不得不添加一个小延迟并立即解决这个问题:

override func cancelInteractiveTransition() {
    if percentComplete > 0.1 {
        updateInteractiveTransition(0)
        super.cancelInteractiveTransition()
    } else {
        var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            self.updateInteractiveTransition(0)
            super.cancelInteractiveTransition()
        })
    }
}

override func finishInteractiveTransition() {
    if percentComplete > 0.1 {
        super.finishInteractiveTransition()
    } else {
        var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
            super.finishInteractiveTransition()
        })
    }
}