and*_*ank 4 easing-functions uiviewanimation ios
如果需要更高级的曲线,UIView基于块的动画方法中缺少自定义缓动曲线会导致Core Animation.
CAKeyframeAnimation在如何使用Core Animation创建自定义缓动功能中讨论了使用Category on执行此操作的方法?.
为了保持我的代码清洁和可维护,我想更进一步,重新实现UIView基于块的方法,并包含一个描述缓动曲线功能的块.结果类别UIView看起来像这样:
+ (void)animateWithDuration:(NSTimeInterval)duration easingCurveFunction:(double(^)(double))function animations:(void (^)(void))animations;
Run Code Online (Sandbox Code Playgroud)
有没有人知道苹果如何实现基于块的动画方法?
我不确定是否创建了基于块的方法,但我发现实现自定义缓动函数并仍然使用基于块的动画的一种相当简单的方法是覆盖图层的addAnimation:(CAAnimation *)anim forKey:(NSString *)key方法并交换不同的缓动函数无论你想要动画的任何视图.
首先,继承CALayer,并添加以下方法......
// CustomViewLayer.m
#import "CustomViewLayer.h"
@implementation CustomViewLayer
- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key
{
if ([anim isKindOfClass:[CABasicAnimation class]]) {
// intercept the animation and insert your own easing function
int x1 = 0.250;
int y1 = 1.185;
int x2 = 0.210;
int y2 = 1.000;
CAMediaTimingFunction *func = [CAMediaTimingFunction functionWithControlPoints:x1 :y1 :x2 :y2];
anim.timingFunction = func;
}
[super addAnimation:anim forKey:key];
}
@end
Run Code Online (Sandbox Code Playgroud)
在您的视图子类中,确保您返回自定义图层类...
// CustomView.m
#import "CustomView.h"
#import "CustomViewLayer.h"
@implementation CustomView
+ (Class)layerClass
{
return [CustomViewLayer class];
}
@end
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用标准的基于块的动画方法,如...
[UIView animateWithDuration:0.3 animations:^{
[customView setFrame:newFrame];
}];
Run Code Online (Sandbox Code Playgroud)
...将使用自定义缓动功能.它可能无法解决您的所有问题,但它很容易做到,它仍然允许您使用基于块的动画.但请记住,缓动功能将适用于通过基于块的方法设置动画的任何和所有动画.因此,如果您想要为alpha属性设置动画,例如,但又不想使用自定义缓动功能,则必须使用它,或者完全提出不同的解决方案.
最后,可以在这里找到一个用于轻松创建和测试缓动函数的强大工具:http://matthewlein.com/ceaser/
| 归档时间: |
|
| 查看次数: |
1809 次 |
| 最近记录: |