使用自定义缓动曲线重新实现基于UIView块的动画方法 [英] Reimplement UIView block based animation methods with custom easing curve

查看:174
本文介绍了使用自定义缓动曲线重新实现基于UIView块的动画方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要更高级的曲线,UIView基于块的动画方法中缺少自定义缓动曲线会导致Core Animation。

The lack of custom easing curves in UIView's block based animation methods leads to Core Animation if more advanced curves are needed.

CAKeyframeAnimation 上的类别执行此操作的方法stackoverflow.com/questions/5161465/how-to-create-custom-easing-function-with-core-animation\">如何使用Core Animation创建自定义缓动功能?

A way of doing this with a Category on CAKeyframeAnimation is discussed in How to create custom easing function with Core Animation?.

为了保持我的代码清洁和可维护,我想更进一步,重新实现 UIView 的基于块的方法,并包含一个描述块缓和曲线功能。 UIView 上的结果类别如下所示:

To keep my code clean and maintainable I would like to go a step further and re-implement UIView's block based methods and include a block describing the easing curve function. resulting category on UIView would look something like this:

+ (void)animateWithDuration:(NSTimeInterval)duration easingCurveFunction:(double(^)(double))function  animations:(void (^)(void))animations;

有没有人知道苹果如何实现基于块的动画方法?

Does anyone have an idea of how apple implements their block based animation methods?

推荐答案

我不确定是否创建了基于块的方法,但我发现这是一种实现自定义缓动函数的相当简单的方法使用基于块的动画是覆盖图层的 addAnimation:(CAAnimation *)anim forKey:(NSString *)key 方法并在不同的缓动函数中交换您想要的任何视图动画。

I'm not sure about creating a block-based method, but I've found that a fairly simple way to implement a custom easing function and still used block-based animations is to override the layer's addAnimation:(CAAnimation *)anim forKey:(NSString *)key method and swap in a different easing function for whatever view you want to animate.

首先,继承CA​​Layer,并添加以下方法......

To start, subclass CALayer, and add the following method...

// 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

在您的视图子类中,make确定你要返回自定义图层类...

In your view subclass, make sure you're returning the custom layer class...

// CustomView.m

#import "CustomView.h"
#import "CustomViewLayer.h"

@implementation CustomView

+ (Class)layerClass
{
    return [CustomViewLayer class];
}

@end

然后,你可以使用标准的基于块的动画方法,如...

Then, you can use the standard block-based animation methods such as...

[UIView animateWithDuration:0.3 animations:^{
    [customView setFrame:newFrame];
}];

...将使用自定义缓动功能。它可能无法解决您的所有问题,但它很容易做到,它仍然允许您使用基于块的动画。但请记住,缓动功能将适用于通过基于块的方法设置动画的任何和所有动画。因此,如果您想要为alpha属性设置动画,但不想使用自定义缓动功能,则必须使用它或者可能会提出完全不同的解决方案。

...and the custom easing function will be used. It may not solve all your problems, but it is easy to do and it still allows you to use block-based animations. Keep in mind, however, that the easing function will apply to any and all animations that are animated through the block-based methods. So if you want to animate the alpha property for example, but don't want to use the custom easing function, you will have to fiddle around with it or maybe come up with a different solution altogether.

最后,可以在此处找到一个用于轻松创建和测试缓动功能的强大工具: http ://matthewlein.com/ceaser/

Lastly, an awesome tool for easily creating and testing easing functions can be found here: http://matthewlein.com/ceaser/

这篇关于使用自定义缓动曲线重新实现基于UIView块的动画方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆