Ios UIView animateKeyframes不工作
我正在尝试一个关键帧动画,其中视图淡入,然后变换(平移和缩放),然后在延迟后再次变换,然后淡出。我遇到的问题是,第二个变换在第一个变换结束后立即开始,而不是考虑延迟值。这似乎只有在再次修改变换时才会发生,因此我怀疑问题来自于尝试在同一关键帧块中执行两个不同的变换更改,尽管我不明白为什么不允许这样做,因为动画持续时间没有重叠 代码如下:Ios UIView animateKeyframes不工作,ios,objective-c,uiview,Ios,Objective C,Uiview,我正在尝试一个关键帧动画,其中视图淡入,然后变换(平移和缩放),然后在延迟后再次变换,然后淡出。我遇到的问题是,第二个变换在第一个变换结束后立即开始,而不是考虑延迟值。这似乎只有在再次修改变换时才会发生,因此我怀疑问题来自于尝试在同一关键帧块中执行两个不同的变换更改,尽管我不明白为什么不允许这样做,因为动画持续时间没有重叠 代码如下: [UIView animateKeyframesWithDuration:15.0 delay:0.0 options:UIViewKeyframeAnimati
[UIView animateKeyframesWithDuration:15.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^
{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.alpha = 1.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.transform = self.imageDataCurrent.transform2;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.4 animations:^
{
self.imageViewCurrent.transform = self.imageDataCurrent.transform3;
}];
[UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.alpha = 0.0;
}];
}
以下是正在使用的转换:
开始转换:
(a=5,b=0,c=0,d=5,tx=950,ty=850)
转型2:
(a=5,b=0,c=0,d=5,tx=1250,ty=1500)
变革3:
(a=2.5,b=0,c=0,d=2.5,tx=-250,ty=0)
更新1:
我没有使用自动布局
更新2:
我已经做了更多的测试,并确定确实是两个变换修改导致了问题。如果将其中一个变换修改替换为alpha,则所有动画计时都正常工作。正如我在这一点上所能说的,这是UIKit动画的一个bug
解决方法:
我发现通过直接使用核心动画,我能够避免这个问题。此代码产生大致相同的效果,但没有错误:
// Fade in, pause, fade out.
CAKeyframeAnimation *alphaAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
alphaAnim.keyTimes = @[@(0.0), @(0.1), @(0.9), @(1.0)];
alphaAnim.values = @[@(0.0), @(1.0), @(1.0), @(0.0)];
alphaAnim.duration = 15.0;
[self.imageViewCurrent.layer addAnimation:alphaAnim forKey:@"opacity"];
// Translate, wait on second translation, translate again.
CAKeyframeAnimation *transformAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
transformAnim.keyTimes = @[@(0.3), @(0.4), @(0.5), @(0.8)];
transformAnim.values = @
[
[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform1)],
[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform2)],
[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform2)],
[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(self.imageDataCurrent.transform3)]
];
transformAnim.duration = 15.0;
[self.imageViewCurrent.layer addAnimation:transformAnim forKey:@"transform"];
听起来你有一个时间轴,有一个动画运行的周期和“休息”。我建议你输入休息作为一个空动画块的关键帧 大概是这样的:
[UIView animateKeyframesWithDuration:15.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^
{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.alpha = 1.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.transform = self.imageDataCurrent.transform2;
}];
//Create an empty keyframe
[UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.3 animations:^
{
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.4 animations:^
{
self.imageViewCurrent.transform = self.imageDataCurrent.transform3;
}];
[UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.1 animations:^
{
self.imageViewCurrent.alpha = 0.0;
}];
}
在iOS 8中,涉及变换的动画确实表现得很奇怪,因此您可能从自己的角度遇到过这种已知的奇怪现象。只是想澄清一下:您使用的是自动布局吗?不幸的是,这对我不起作用,第二个变换动画仍然在第一个变换动画结束后立即开始。