如何做一个CALayer-自定义属性CAAnimation动画

本文探讨了如何在iOS开发中自定义CALayer属性并实现动画。介绍了CALayer作为键值编码容器类的特性,@dynamic在CALayer中的作用,以及initWithLayer:方法在创建动画副本时的重要性。此外,文章还详细讲解了如何设置自定义属性动画,并提到了隐式动画的概念。通过示例代码展示了如何创建和应用这些动画效果。
摘要由CSDN通过智能技术生成

最近在做CALayer自定义属性(@property修饰)动画。之前没有深究过,现在有很多有意思的发现,写出来分享给大家。

首先把Demo LNLoadingLayer分享给大家,这是一个loading动画。

在做CALayer自定义属性动画之前,要先了解一个概念,即CALayer是一个符合键值编码的容器类(Key-Value Coding Compliant Container Classes)

符合键值编码的容器类

CALayer和CAAnimation都是符合键值编码的容器类(Key-Value Coding Compliant Container Classes),

这意味着你可以用任意key来设置值,即使这个key没有声明为CALayer的property。

以下是苹果Core Animation 编程指南的介绍:

TheCAAnimationandCALayer classes are key-value coding compliant container classes, which means that you can set values for arbitrary keys. Even if the keysomeKeyis not a declared property of theCALayerclass, you can still set a value for it as follows:

[ theLayer setValue:[NSNumber numberWithInteger:50] forKey:@“someKey”];

You can also retrieve the value for arbitrary keys like you would retrieve the value for other key paths. For example, to retrieve the value of thesomeKeypath set previously, you would use the following code:

someKeyValue = [theLayer valueForKey:@“someKey”];

因此,在CALayer中,用@dynamic修饰的属性,即使没有实现setter getter也不会崩溃。

@interface DynamicTest : CALayer
@property NSString *title;
@end

@implementation DynamicTest
@dynamic title;

@end

DynamicTest *test = [[DynamicTest alloc] init];
test.title = @"title";//正常工作,不会崩溃。
//另外,CALayer和CAAnimation,会自动为@dynamic修饰的属性添加NSCoding协议的支持。不用@dynamic修饰,则不支持。
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:test];
DynamicTest *unarchiveTest = [NSKeye
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值