隐式动画

2020-04-01  本文已影响0人  若水water

动画的体现分为显示动画和隐式动画,在实现上分为核心动画,和UIView 动画。
当你改变CALyer的一个可做动画的属性,它并不能立刻在屏幕上体现出来。相反,,它是从先前的值平滑过渡到新的值。这一切都是默认的行为,你不需要做额外的操作。如下,在红色背景的view上放一个 带图片的layer。

UIView *bagView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 200)];
    bagView.backgroundColor = UIColor.redColor;
    [self.view addSubview:bagView];
    _bagView = bagView;
//    意味着自图层会在左下角坐标开始排版,而不是左上角
//    _bagView.layer.geometryFlipped = YES;
    _bagView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    _bagView.layer.cornerRadius = 10;
//    翻转
    _subLayer = [HLDrawTestLayer layer];
    _subLayer.frame = CGRectMake(0, 0, 80, 80);
    _subLayer.backgroundColor = UIColor.blueColor.CGColor;
    _subLayer.position = CGPointMake(150, 100);
    [_bagView.layer addSublayer:_subLayer];

    UIImage *image = [UIImage imageNamed:@"123.jpg"];
    //    转换成id 类型 ,必须为图片
    _subLayer.contents = (__bridge id)image.CGImage;
    //    设置 图片的 缩放模式
    _subLayer.contentsGravity = kCAGravityResizeAspect;
//放一个按钮,改变层的 可动画属性
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    button.backgroundColor = UIColor.blueColor;
    button.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 100);
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

以上代码中的layer 是自定义的 HLDrawTestLayer ,在这个类中,我们实现两个方法:


@implementation HLDrawTestLayer

- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key {
    [super addAnimation:anim forKey:key];
    NSLog(@"test - anim = %@",[anim debugDescription]);
}

//改变属性的时候 都会调用这个方法
- (id<CAAction>)actionForKey:(NSString *)event {
    id action = [super actionForKey:event];

    NSLog(@"event = %@, action = %@",event,[action debugDescription]);
    return action;
}

@end

按钮的触发方法:

- (void)buttonClick {
//改变锚点
      _subLayer.anchorPoint = CGPointMake(1, 1);
     NSLog(@"position2 = %@,anchorpoint = %@",NSStringFromCGPoint(_subLayer.position),NSStringFromCGPoint(_subLayer.anchorPoint));
}

运行结果:

//layer的 actionForkey:方法 结果,可见返回了一个CABasicAnimation动画,这个不是我们手动创建,而是系统自己生成。
event = anchorPoint, action = <CABasicAnimation:0x600001b7a760; fillMode = backwards; timingFunction = default; keyPath = anchorPoint; fromValue = NSPoint: {0.5, 0.5}>

//addAnimation:forkey方法 结果,将返回的CABasicAnimation 添加到了层上,所以会看到改变可动画属性的值,会平滑的改变,而不是立即生效
test - anim = <CABasicAnimation:0x600001b7a760; fillMode = backwards; timingFunction = default; keyPath = anchorPoint; fromValue = NSPoint: {0.5, 0.5}>

其实以上就是所谓的隐式动画,之所以叫隐式动画是因为我们并没有指定任何动画的类型。我们仅仅改变了一个属性。然后Core Animation 来决定如何并且何时去做动画。
当你改变一个属性,Core Animation 是如何判断动画类型和持续时间的呢?实际上动画执行的时间取决于当前事务的设置,动画类型取决于图层行为。事务实际上是Core Animation 用来包含一系列属性动画集合的机制,任何用指定事务去改变可以做动画的图层属性都不会立刻发生变化,而是当事务一旦提交的时候开始用一个动画过度到新值。
事务是通过CATranscation类来做管理,管理了一堆你不能访问的事务,CATranscation没有属性或者实例方法,并且也不能用+alloc 和-init方法创建它,但是可以用+begin 和 +commit分别来入栈或者出栈。
任何可以做动画的图层属性都会被添加到栈顶的事务,你可以通过+setAnimationDuration:方法设置当前事务的动画时间,或者通过+animationDuration方法来获取值,默认0.25秒

@implementation HLDrawTestLayer

- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key {
    [super addAnimation:anim forKey:key];
    NSLog(@"test - anim = %@,duriation = %lf",[anim debugDescription],[CATransaction animationDuration]);
}
@end

打印结果:(duriation = 0.250000)
 test - anim = <CABasicAnimation:0x600003492c40; fillMode = backwards; timingFunction = default; keyPath = anchorPoint; fromValue = NSPoint: {0.5, 0.5}>,duriation = 0.250000

Core Animation 在每个run loop周期中自动开始一次新的事务,即使你不显示的调用[CATransaction begin]开始一次事务,任何一次runloop循环中属性的改变都会被集中起来,然后做一次0.25s的动画
所以明白以上原理后,怎样修改动画的时间,调用当前事务的 +setAnimationDuration:方法来修改动画时间,但在这里我们首先起一个新的事务,这样修改时间就不会有别的副作用,因为修改当前事务的时间可能会导致同一时刻别的动画(如屏幕旋转,所以一般系统动画默认时间是0.25s,),所以最好还是在调整动画之前压人一个新的事务。对以上button事件 做如下修改:

- (void)changeTime {
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    _subLayer.anchorPoint = CGPointMake(1, 1);
    [CATransaction commit];  
}

运行程序后,会发现比原来移动的更慢了。
如果用过UIView的动画方法做过一些动画效果,那么应该对这个模式不陌生。UIView有两个方法,+beginAnimations:context:+commitAnimations,和CATransaction+begin+commit 方法类似,实际上在+beginAnimations:context:+commitAnimations直接所有视图或者图层属性的改变而做的动画都是由于设置了CATransaction的原因。
在iOS4中,苹果对UIView添加了一种基于block的动画方式:+animationWithDuration:animations:这样写对做一堆的属性动画在语法上会更加简单,但实质上他们都是在做同样的事情。内部封装了 CATransaction+begin+commit方法,这样block中所有属性的改变都会被事务所包含。这样也可以避免开发者由于对+begin+commit匹配的失误而造成的风险。
UIview的block 允许你在动画完成的时候提供一个完成的动作,CATranscation提供的+setCompletionBlock:方法也有同样的功能,对上述代码修改,添加一个完成操作:

- (void)changeTime {
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
//动画完成后,顺时针旋转90度
    [CATransaction setCompletionBlock:^{
        CGAffineTransform transform = _subLayer.affineTransform;
        transform = CGAffineTransformRotate(transform, M_PI_2);
        _subLayer.affineTransform = transform;
    }];
    _subLayer.anchorPoint = CGPointMake(1, 1);
    [CATransaction commit];  
}
图层行为

以上例子中,我们对单独的图层 修改可动画属性,会默认执行系统动画0.25s,使用提交事务的方式,可以修改动画的时间。接下来我们对UIView关联的图层做动画而不是一个单独的图层.对以上 button事件做修改,对view的图层属性修改:(_subLayer 换成_bagView.layer)

- (void)changeTime {
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    [CATransaction setCompletionBlock:^{
        CGAffineTransform transform = _bagView.layer.affineTransform;
        transform = CGAffineTransformRotate(transform, M_PI_2);
        _bagView.layer.affineTransform = transform;
    }];
    _bagView.layer.anchorPoint = CGPointMake(1, 1);
    [CATransaction commit];
}

运行程序会发现,位置是瞬间改变的,并没有动画过度,隐式动画都没有起作用,好像是被UIView关联的图层给禁用了。
前一章提过对UIVIew 属性的修改,就是对它所关联的层的属性的修改,所以如果该属性是可动画属性的话,那么无论在什么时候修改它,我们都应该能注意到的。所以说UIKit建立在Core Animation之上,那么隐式动画是如何被UIKit禁用的呢?
我们知道Core Animation通常对CALayer的所有属性(可动画的属性)做动画,但是UIView把它关联的图层这个特性给关闭了。为了更好说明这一点,我们需要知道隐式动画是如何实现的。

隐式动画的实现

我们改变属性时CALayer自动应用的动画称作行为,当CALayer的属性呗修改的时候,它会调用-actionForKey:方法,传递属性的名称。

@implementation HLDrawView
//当属性改变时,layer的代理会调用此方法,
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {
    id obj = [super actionForLayer:layer forKey:event];
    NSLog(@"obj = %@",obj);
    return obj;
}
@end

//创建这样一个view
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HLDrawView *testView = [[HLDrawView alloc]init];
 //改变属性,观察输出
    testView.frame = CGRectMake(0, 0, 300, 200);
    testView.backgroundColor = UIColor.grayColor;
    testView.center = self.view.center;
    [self.view addSubview:testView];
    _subView = testView;
}

运行程序,看打印结果:(可以看到全部是null,所以此时不会有动画产生)

2020-04-01 16:00:35.092313+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.092564+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.092719+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.092848+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.092972+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093108+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093296+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093432+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093644+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093766+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.093891+0800 AnimationTest[8466:3612248] obj = <null>
2020-04-01 16:00:35.105964+0800 AnimationTest[8466:3612248] obj = <null>

接着我们加一个按钮,点击事件中,提交一个事务,对testView 属性修改

- (void)buttonClick2 {
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    _subView.center = CGPointMake(0, 0);
    [CATransaction commit];
}

观察打印输出:(结果一样是null,也是不会有动画产生)

2020-04-01 16:04:42.135850+0800 AnimationTest[8535:3615332] obj = <null>

接着放到UIView动画块中:

- (void)buttonClick2 {
    [UIView animateWithDuration:0.3 animations:^{
        _subView.center = CGPointMake(0, 0);
    }];
}

观察打印结果:(此时结果不为空,产生了个行为)

2020-04-01 16:07:39.634071+0800 AnimationTest[8582:3617609] obj = <_UIViewAdditiveAnimationAction: 0x6000036b7720>

此时UIView会把这个结果给layer,layer的actionForKey:方法就会返回一个不为空的Animation对象,并添加到自身,实现动画。

关闭隐式动画的另一个方式

除了让代理实现的-actionForLayer:forKey方法返回nil,CATransaction有一个方法+setDisableActions:,可以对它所管理的所有可动画属性打开或者关闭隐式动画。以下代码也可以关闭隐式动画

[CATransaction setDisableActions:YES];

修改隐式动画

呈现与模型

CALayer的属性行为其实很不正常,因为改变一个图层的属性并没有立刻生效,而是通过一段时间渐变更新。当你改变一个图层的属性,属性值的确是立刻更新的(如果你读取它的数据,你会发现它的值在你设置那一刻就已经生效了),但是屏幕上并没有马上发生改变,这是因为你设置的属性并没有直接跳转图层的外观,相反,他只是定义了图层动画结束之后将要变化的外观。
当设置CALayer的属性,实际上是在定义当前事务结束之后图层如何显示的模型,Core Animation 扮演了一个控制器的角色,并且负责根据图层行为和事务设置去不断更新视图的这些属性在屏幕上的状态。
在iOS中,屏幕每秒重绘60次,如果动画时长比1/60要长,core Animation就需要在新值和新值生效之间,对屏幕上的图层进行重新组织。这意味着CALayer除了真实值(就是你设置的值)之外,必须要知道当前显示在屏幕上的属性值的记录。
每个图层属性的显示值都被存储在一个叫做程序图层的独立图层当中,他可以通过-presentationLayer方法来访问。这个呈现图层实际上是模型图层的赋值,但是它的属性值代表了在任何指定时刻当前外观效果。换句话说,你可以通过呈现图层的值来获取当前屏幕上真正显示出来的值。
呈现树通过图层树中所有图层的呈现图层所形成。注意呈现图层仅仅当图层首次被提交(就是首次第一次在屏幕上显示)的时候创建,所以在那之前调用-presentationLayer返回的都是nil。
你可能注意到一个叫做–modelLayer的方法。在呈现图层上调用–modelLayer将会返回它正在呈现所依赖的CALayer。通常在一个图层上调用–modelLayer会返回self

image.png

一个移动的图层是如何通过数据模型呈现的。

使用呈现图层

大多数情况下,你不需要直接访问呈现图层,你可以通过和模型图层的交互来让core Animation更新显示。两种情况下呈现图层会变得很有用,一个是同步动画,一个是处理用户交互

@interface ViewController ()

@property (nonatomic, strong) CALayer *colorLayer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //create a red layer
    self.colorLayer = [CALayer layer];
    self.colorLayer.frame = CGRectMake(0, 0, 100, 100);
    self.colorLayer.position = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:self.colorLayer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //get the touch point
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //check if we've tapped the moving layer
    if ([self.colorLayer.presentationLayer hitTest:point]) {
        //randomize the layer background color
        CGFloat red = arc4random() / (CGFloat)INT_MAX;
        CGFloat green = arc4random() / (CGFloat)INT_MAX;
        CGFloat blue = arc4random() / (CGFloat)INT_MAX;
        self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    } else {
        //otherwise (slowly) move the layer to new position
        [CATransaction begin];
        [CATransaction setAnimationDuration:4.0];
        self.colorLayer.position = point;
        [CATransaction commit];
    }
}

参考链接:
https://www.jianshu.com/p/bb3c60acde4b

上一篇 下一篇

猜你喜欢

热点阅读