gpt4 book ai didi

cocoa-touch - 为什么 animateWithDuration 动画和完成 block 之间的暂停?

转载 作者:行者123 更新时间:2023-12-04 12:48:56 24 4
gpt4 key购买 nike

按照 Apple 的建议,我通过对 -animationWithDuration:animation: 进行后续调用来链接 UIView 动画。在 completion:阻止另一个调用 aanimateWithDuration:animation:completion: ,像这样:

[UIView animateWithDuration:scaleDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
// Scale the controllers' views down.
self.view.transform = CGAffineTransformScale(self.view.transform, 0.8, 0.8);
} completion:^(BOOL finished) {
// Transition to the new view and push on the new view controller.
[UIView transitionWithView:self.view duration:1 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self pushViewController:viewController animated:NO];
} completion:^(BOOL finished) {
[UIView animateWithDuration:scaleDuration delay:0 options:UIViewAnimationOptionCurveLinear animations:
^{
// Scale back to the original size.
self.view.transform = CGAffineTransformScale(self.view.transform, 1.25, 1.25);
} completion:nil];
}];
}];

动画都按正确的顺序执行,但它们之间有一点延迟,尤其是在 -transitionWithView:duration:options:animations:completion: 之前。称呼。如何平滑动画步骤之间的过渡?

最佳答案

在旁边:
您以这种方式滥用导航 Controller 有什么特别的原因吗?你不能只用presentViewController:animated:completion:将其过渡样式设置为 UIModalTransitionStyleFlipHorizontal ?

回到你的问题:

我很确定口吃来自 pushViewController:animated: 这个简单的事实。隐式必须加载要推送的 View Controller 的 View ,这需要一些时间。

所以如果你不能使用 presentViewController:animated:completion: (或 presentModalViewController:animated: 如果你必须支持 iOS 4)方法,我鼓励你试试这个:

// ensure the view is already loaded when you invoke `pushViewController:animated:`
[viewController view];

// there is no strict need to calculate those in situ, so we do it up front
CGAffineTransform originalTransform = self.view.transform;
CGAffineTransform downscalingTransform = CGAffineTransformScale(originalTransform, 0.8, 0.8);

// I found it hard to read the block-in-a-block-in-a... while editing here, so I've taken them apart:
void (^scaleDownAnimation)() = ^{
self.view.transform = downscalingTransform;
};

void (^restoreScaleAnimation)() = ^{
self.view.transform = originalTransform;
};

void (^pushControllerAnimation)() = ^{
[self pushViewController:viewController animated:NO];
};

void (^pushAnimationCompletion)(BOOL) = ^(BOOL unused) {
[UIView animateWithDuration:scaleDuration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:restoreScaleAnimation
completion:nil];
};

void (^downscaleCompletion)(BOOL) = ^(BOOL unused){
UIViewAnimationOptions linearFlipFromLeft = UIViewAnimationOptionCurveLinear | UIViewAnimationOptionTransitionFlipFromLeft;
[UIView transitionWithView:self.view
duration:1
options:linearFlipFromLeft
animations:pushControllerAnimation
completion:pushAnimationCompletion];
};

[UIView animateWithDuration:scaleDuration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:scaleDown
completion:downscaleCompletion];

备注 牛肉在前六行之内,其余的只是为了完整性。

关于cocoa-touch - 为什么 animateWithDuration 动画和完成 block 之间的暂停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468333/

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