【发布时间】:2011-11-13 17:29:01
【问题描述】:
View Programming Guide for iOS 告诉我们,基于块的动画是前进的方向,而不是现在几乎已弃用的 begin/commit 样式动画:
注意:如果您正在为 iOS 4 或更高版本编写应用程序,则应使用基于块的方法来为您的内容设置动画。有关如何使用这些方法的信息,请参阅“Starting Animations Using the Block-Based Methods.”
但现在我需要使用自定义计时函数CAMediaTimingFunction,所以我求助于使用CATransactions 和CABasicAnimations。这些类使用与已弃用的 UIView 动画样式相同的语义语言,使用 [CATransaction begin] 和 [CATransaction commit] 等方法。在其他一切都是基于块的应用程序中间感觉很奇怪。
有没有办法将CAMediaTimingFunctions 等概念与基于块的动画结合起来?
更新 1:
我想“阻塞”的一段示例代码如下所示:*
[CATransaction begin];
{
[CATransaction setValue:[NSNumber numberWithFloat:3.0f] forKey:kCATransactionAnimationDuration];
CGPoint low = CGPointMake(0.150, 0.000);
CGPoint high = CGPointMake(0.500, 0.000);
[CATransaction begin];
{
CAMediaTimingFunction* perfectIn = [CAMediaTimingFunction functionWithControlPoints:low.x :low.y :1.0 - high.x :1.0 - high.y];
[CATransaction setAnimationTimingFunction: perfectIn];
CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeIn.fromValue = [NSNumber numberWithFloat:0];
fadeIn.toValue = [NSNumber numberWithFloat:1.0];
[viewB.layer addAnimation:fadeIn forKey:@"animateOpacity"];
}
[CATransaction commit];
}
[CATransaction commit];
更新 2
我为我的另一个问题制作了一个示例项目,其中包含上面的代码。 It's on github.
【问题讨论】:
-
如何在
UIView上创建一个类别,方法是将CAAnimation内容包装在一个漂亮的块中? -
感觉就像在重新发明轮子,但如果这个问题没有得到答案,我就会求助。
-
由于 CABasicAnimations,它也将变得相当困难。想试试写一个吗,@Paul.s?
-
如果你愿意给我一个样品(预块)开始我会去...
标签: objective-c cocoa-touch cocoa core-animation objective-c-blocks