尝试使用两个动画为一个UIImageView设置动画,只有一个动画具有持续时间

时间:2013-02-28 21:56:27

标签: ios objective-c cocoa-touch cgaffinetransform

我想要做的是立即翻转此UIImageView,不动画,然后我需要它float到它在屏幕上的预定位置。 transformations都有效,但不是我想要的方式。

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
resultsEnemy.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];

resultsEnemy.transform = CGAffineTransformMakeScale(-1, 1);

这是我正在使用的代码。尽管缩放代码(我用来翻转UIImageView)不是0.5 duration动画的一部分,但它遵循这些规则。我该如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

应用这样的两个变换不会产生您期望的结果。您需要做的是将它们组合成一个变换矩阵。以下内容应按预期工作。

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

// Create two separate transforms and concatenate them together.
// Use that new transform matrix to accomplish both transforms at once.
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
CGAffineTransform scale = CGAffineTransformMakeScale(-1, 1);
resultsEnemy.transform = CGAffineTransformConcat(translate, scale);

[UIView commitAnimations];

编辑:根据您的澄清,您似乎想要这样的事情:

CGAffineTransform scale = CGAffineTransformMakeScale(-1, 1);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
resultsEnemy.transform = scale;
[CATransaction commit];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

resultsEnemy.transform = CGAffineTransformConcat(translate, scale);

[UIView commitAnimations];