POP动画引擎中Layer与CALayer的一点区别

POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步:

https://github.com/facebook/pop

下面简单的讲一下POP动画引擎中Layer与CALayer的区别:

这里, 代码做的都是同一个效果: 执行位移动画3秒, 然后在1秒后移除所有动画

使用POP效果图:

使用CALayer效果图:

可以看到, POP执行的动画当动画被移除之后, 被执行对象保留了被停止时的状态

而CALayer执行的动画当动画被移除之后, 跳到了另外一个状态, 这是因为CALayer添加的

CABasicAnimation动画会另外生成一个图层来执行动画, 而CALayer本身早已经是动画结束后状态, 当动画结束后就会显示出来,

所以就会出现, 当动画执行到一半被移除的时候, 就会出现跳动的现象..

这也就是这两者之间的一点区别..

 

以下是代码:

#import "ViewController.h"
#import <POP.h>

@interface ViewController ()

// 测试CALayer
@property (nonatomic, strong) CALayer *normalLayer;
// 用于测试POP的Layer
@property (nonatomic, strong) CALayer *popLayer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self setupNormalLayer];
    
    // 一秒后移除所有动画
    [self performSelector:@selector(stopAnimation) withObject:nil afterDelay:1.f];
}

- (void)setupPopLayer
{
    // 初始化
    self.popLayer = [CALayer layer];
    self.popLayer.frame = CGRectMake(0, 0, 100, 100);
    self.popLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.popLayer];
    // 添加POP动画
    POPBasicAnimation *basicAnimation = [POPBasicAnimation animationWithPropertyNamed:@"position"];
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.popLayer.position.x, 300)];
    basicAnimation.duration = 3.f;
    [self.popLayer pop_addAnimation:basicAnimation forKey:nil];
}

- (void)setupNormalLayer
{
    // 初始化
    self.normalLayer = [CALayer layer];
    self.normalLayer.frame = CGRectMake(0, 0, 100, 100);
    self.normalLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.normalLayer];
    // 添加动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.normalLayer.position.x, self.normalLayer.position.y)];
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.normalLayer.position.x, 300)];
    basicAnimation.duration = 3.f;
    [self.normalLayer addAnimation:basicAnimation forKey:nil];
    
    self.normalLayer.position = CGPointMake(self.normalLayer.position.x, 300);
}

- (void)stopAnimation
{
    [self.normalLayer removeAllAnimations];
//    [self.popLayer pop_removeAllAnimations];
}

@end
原文地址:https://www.cnblogs.com/Rinpe/p/5158930.html