美文网首页
CoreAnimation之自定义动画属性

CoreAnimation之自定义动画属性

作者: 充满活力的早晨 | 来源:发表于2018-10-10 16:47 被阅读13次

看完苹果文档,我们知道layer可以自定义属性动画的。但是具体怎么实现自定义动画的属性,苹果并没有举例。因此这里做个例子供大家参考。

知识回顾

coreAnimation的应用程序有三组layer 对象

layer tree:(模型树),存储的是目标值
presentation tree:(表现树)正在运行的值
render tree:(渲染树)正在执行的值。


隐式动画:隐式动画使用默认的计时和动画属性来执行动画。
隐式动画修改的是layer tree(模型树)的内容
显示动画:显式动画则需要我们是用动画对象自己配置这些属性。而显示动画是修改的是presentation tree(变现树)的内容。

不管layer的属性执行显式动画还是隐式动画,其实都是绑定了一个CAAction对象。真正执行动画的是CAAction

layer动画查找action循环

  • 1.如果layer有delegate对象,并且delegate对象实actionForLayer:forKey:方法。layer将调用该方法。delegate必须做下列行为之一

返回该key的action对象
如果不处理该action,返回nil,在该种情况下,coreAnimation还将继续搜索。
返回一个NSNull 对象,coreAnimation 将结束搜索。

  • 2.查看layer的action字典,是否有该key对应的action
  • 3.layer 从style 字典中继续查找是否包含该key的action
  • 4.layer调用方法 defaultActionForKey:方法
  • 5.如果找到了就执行coreAnimation定义的隐式动画。
    如果我们在任何比较适合的搜索点提供了action对象。layer将停止搜索并执行该操作对象。当coreAnimation找到了action对象,layer将调用action对象的runActionForKey:object:arguments:方法来执行操作。

安装action对象位置取决于我们打算如何修改layer

  • 对于仅是在特定情况下使用的action,或者已经使用delegate的layer,我们就在delegate中实现actionForLayer:forKey:方法 就可以了
  • 如果layer没有使用delegate,我们将action添加到layer的actions字典中就行了
  • 对于关联layer普通属性的action,我们在style字典中包含该属性就可以了。
  • 对于layer很重要的操作,我们生成一个子类实现defaultActionForKey:方法

+ (BOOL)needsDisplayForKey:(NSString *)key

这个函数其实很重要了

/* Method for subclasses to override. Returning true for a given
 * property causes the layer's contents to be redrawn when the property
 * is changed (including when changed by an animation attached to the
 * layer). The default implementation returns NO. Subclasses should
 * call super for properties defined by the superclass. (For example,
 * do not try to return YES for properties implemented by CALayer,
 * doing will have undefined results.) */

这是苹果英文文档

个人理解,要是我们属性想执行自定义动画,该属性的key在这里就应该返回YES。意思是,我们声明的属性是动画属性。
这里还需要注意的是,因为我们声明改属性是动画属性,系统会随时更改该属性的值,其实可以理解为该属性的值对应的是presentation tree(表现树)。
该函数要是我们返回的值是NO,那么就代表对应的是layer tree。

属性声明

我们应该将代表presentation tree 的当前值的属性声明成dynamic。在运行时生成set get 方法


搞明白这里我们就做个demo看看。

@interface MYLayer : CALayer
@property (nonatomic,assign) CGFloat radius;
@end
#import <UIKit/UIKit.h>
@interface MYLayer()
@property (nonatomic,assign) CGFloat displayRadius;
@end
@implementation MYLayer

@dynamic displayRadius;

-(void)setRadius:(CGFloat)radius{
    if (_radius !=radius) {
        _radius = radius;
        self.displayRadius = radius;
    }
}

- (void)drawInContext:(CGContextRef)ctx{
//    NSLog(@"%f",self.radius);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGFloat radius =self.displayRadius;
    CGRect rect;
    rect.size = CGSizeMake(radius * 2, radius * 2);
    rect.origin.x = (self.bounds.size.width - radius * 2) / 2;
    rect.origin.y = (self.bounds.size.height - radius * 2) / 2;
    CGContextAddEllipseInRect(ctx, rect);
    CGContextFillPath(ctx);
}
//
+ (BOOL)needsDisplayForKey:(NSString *)key{
    if ([key isEqualToString:@"displayRadius"]) {
        return YES;
    }
    return [super needsDisplayForKey:key];
}

-(id<CAAction>)actionForKey:(NSString *)event{
    if ([event isEqualToString:@"displayRadius"]) {
        NSLog(@"%lf",self.radius);
        CABasicAnimation * theAnimation;
        theAnimation = [[CABasicAnimation alloc] init];
                theAnimation.duration = 3;
                theAnimation.fromValue = @0;
                theAnimation.toValue =@(self.radius);
        return theAnimation;
    }
    return [super actionForKey:event];
}
//

调用

 - (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        MYLayer * layer= self.layer;
        layer.radius = 200;


    }
    return self;
}



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

这里的displayRadius 代表presentation tree(表现树)
而radius 代表 layer tree(模型树)。最终结果
这里我们重写了setRadius方法,让presentation tree(表现树)和 layer tree(模型树)最终行为一致。

CATransaction

这里不做详细讲解CATransaction ,发现了一个有趣的写法,分享下。
我们知道CATransaction 和CALayer 一样都实现了键值编码。因此我们可以这样写

[CATransaction setValue:@YES forKey:@"byebye"];

将CATransaction 作为一个字典进行值的保存和读取。
看上面一句代码一脸懵逼。看具体例子。

@interface MYLayer : CALayer
@property (nonatomic,assign) CGFloat radius;
@end

#import <UIKit/UIKit.h>
@interface MYLayer()
@property (nonatomic,assign) CGFloat displayRadius;
@end
@implementation MYLayer

@dynamic displayRadius;

-(void)setRadius:(CGFloat)radius{
    if (_radius !=radius) {
        _radius = radius;
        self.displayRadius = radius;
    }
}

- (void)drawInContext:(CGContextRef)ctx{
//    NSLog(@"%f",self.radius);
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
    CGFloat radius =self.displayRadius;
    CGRect rect;
    rect.size = CGSizeMake(radius * 2, radius * 2);
    rect.origin.x = (self.bounds.size.width - radius * 2) / 2;
    rect.origin.y = (self.bounds.size.height - radius * 2) / 2;
    CGContextAddEllipseInRect(ctx, rect);
    CGContextFillPath(ctx);
}
//
+ (BOOL)needsDisplayForKey:(NSString *)key{
    if ([key isEqualToString:@"displayRadius"]) {
        return YES;
    }
    return [super needsDisplayForKey:key];
}

-(id<CAAction>)actionForKey:(NSString *)event{
    if ([event isEqualToString:@"displayRadius"]) {
     NSNumber * num =   [CATransaction valueForKey:@"layerRadius"];
        CABasicAnimation * theAnimation;
        theAnimation = [[CABasicAnimation alloc] init];
        theAnimation.duration = 3;
        theAnimation.fromValue = @0;
        theAnimation.toValue =num;
        return theAnimation;
    }
    return [super actionForKey:event];
}

        MYLayer * layer= [MYLayer layer];
        [self.layer addSublayer:layer];
        [CATransaction begin];
        [CATransaction setValue:@300 forKey:@"layerRadius"];
        layer.position = self.center;
        layer.bounds = self.bounds;
        layer.radius = 200;
        [CATransaction commit];

这里我们通过CATransaction 事务将值传给了MYLayer类。不过这样耦合性太高。这只是个例子啦。

相关文章

网友评论

      本文标题:CoreAnimation之自定义动画属性

      本文链接:https://www.haomeiwen.com/subject/abzuaftx.html