Core Animation Study Notes

1, Core Animation framework



  • CAScrollLayer  is  CALayer  subclass, simplified display part of the layer. CAScrollLayer  range of objects scroll area defined on its inside sublayers. CAScrollLayer not provide a keyboard or mouse event handling, nor does it provide a visible scroll bars.
  • CATextLayer  can easily create a layer class content from the contents of the string or strings. 
  • CATiledLayer  allow incremental display large and complex picture.
  • CAEAGLLayer  provides a OpenGLES rendering environment.
  • CATransition  provides a layer change transition effects, it can affect the entire contents of the layer. When fade animation fade (fade), push (push), revealing the contents (reveal) layer. These transition effects can be extended to your own custom Core Image filters (CIFilter, ios not yet available ).
  • CAAnimationGroup  allows subsequent animation together, parallel display animation.
  • CAPropertyAnimation  is an abstract subclass that supports the critical path specified in the display layer properties in animation
  • CABasicAnimation  simply provide modifications to the layer's properties.
  • CAKeyframeAnimation  support for key frame animation, you can specify the layer properties of the critical path animation, including animation value at each stage, as well as a series of keyframes time value and timing features. Runtime animation, each value is substituted for the specific interpolated value



CAConstraint   class is a layout manager for geometric properties (left, right, top or bottom edges or the horizontal or vertical center) of the relationship described layer.

CATransaction  in Core Animation in charge of coordinating multiple animations inside atomic update the display operation, each layer's attributes modify the animation must be part of a transaction.

Implicit and explicit transaction Affairs
Animation properties are modified in the layer of a thread, while the thread automatically submitted when the next iteration of the modified when implicit transaction is created automatically. Explicit transaction occurs prior to modify the animation program attribute  CATransaction  sends a start message, the animation properties

The message is submitted after the modification.

2, layer, layer

Layer  anchorPoint  property is a  CGPoint  value that specifies a layer based  bounds  position in line with the position of the coordinate system. Anchor (anchor point) specified  bounds  with respect to the  position (corresponding to the view center)  value, while also serving as a fulcrum conversion time . Use of the anchor cell space coordinate system, (0.0,0.0) layer closer to the origin point, and (1.0, 1.0) is a corner origin




3, transformation matrix
CATransform3D   transformation matrix 4 * 4
CGAffineTransform  affine matrix  [abcd tx ty]

Core Animation expanded key - value coding protocol, allows to get and set the critical path of a layer  CATransform3D  value matrix. Table 4 describes the layers  transform  and  sublayerTransform  respective critical path attributes.

Table 4  CATransform3D key paths

You can not specify a method by means of Objective-C attributes 2.0 critical path field structure . The following code can not be executed properly: :
 myLayer.transform.rotation.x=0;

Alternative approach is that you must pass setValue: forKeyPath: or valueForKeyPath: method, as follows:

 [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];

4, each UIView CALayer will automatically create an instance of the class, and then assign layer properties



maskToBounds property determines whether the sub-layer relative to the parent layer cut


5, to CALayer provide content



By delegat: // general view layer is located
displayLayer: 或 drawLayer:inContext:

The above trigger callback
setNeedsDisplay, setNeedsDisplayInRect :, needsDisplayOnBoundsChange or the property of the layer to YES.


- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
     CGMutablePathRef thePath = CGPathCreateMutable();
     
     CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
     
     CGPathAddCurveToPoint(thePath,
                           NULL,
                           15.f,250.0f,
                           295.0f,250.0f,
                           295.0f,15.0f);
     
     
     CGContextBeginPath(theContext);
     CGContextAddPath(theContext, thePath);
     CGContextSetLineWidth(theContext,
                           
                           [[theLayer valueForKey:@"lineWidth"] floatValue]);
     CGContextStrokePath(theContext);
     
     // release the path
     CFRelease(thePath);
}

6、修改图层内容位置
layer的contentsGravity属性








7、 图层Action
当一个行为触发器发生的时候,图层的actionForKey:方法被调用。此方法返回一个行为对象(满足 CAAction协议的对象),对应的标识符作为参数,或如果行为对象不存在的话返回nil。

    当CALayer为一个标识符实现的actionForKey:方法被调用的时候,以下的搜索模式将会被用到:

  1. 如果一个图层有委托,那方法actionForLayer:forKey:的实现将会被调用,把图层和行为标识符作为参数。委托的actionForLayer:forKey:的实现需要响应如下:
    • 返回一个行为标识符对应的行为对象。
    • 返回nil,当无法处理行为标识符的时候。
    • 返回NSNull,当无法处理行为标识符,而且搜索需要被终止的时候。
  2. 图层的actions字典被搜索以便找到一个和行为标识符对应的对象。
  3. 图层的style属性被搜索以便找到一个包含行为标识符的actions字典。
  4. 图层类发生一个defaultActionForKey:的消息。它将会返回一个和标识符对应的行为对象,如果不存在的话则返回nil。

通常行为对象是CAAnimation的子类实例,它实现了CAAction协议。然而你也可以返回任何实现了CAAction协议的类对象。当实例收到runActionForKey:object:arguments:的消息时,它需要执行相应的行为。

代码 1  runActionForKey:object:arguments: 的实现:启动动画

- (void)runActionForKey:(NSString *)key
                 object:(id)anObject
              arguments:(NSDictionary *)dict
{
     [(CALayer *)anObject addAnimation:self forKey:key];
}

重载隐式动画

通过插入一个CAAnimation的实例到style字典里面的actions的字典里面,通过实现委托方法actionForLayer:forKey:或者继承图层类并重载defaultActionForKey:方法返回一个相应的行为对象。
代码2的示例通过委托替换contents属性的隐式动画。
- (id<CAAction>)actionForLayer:(CALayer *)theLayer
                        forKey:(NSString *)theKey
{
    CATransition *theAnimation=nil;

    if ([theKey isEqualToString:@"contents"])
    {

        theAnimation = [[CATransition alloc] init];
        theAnimation.duration = 1.0;
        theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        theAnimation.type = kCATransitionPush;
        theAnimation.subtype = kCATransitionFromRight;
    }

    return theAnimation;
}

代码3的示例使用actions字典模式禁用sublayers属性的默认动画。
// get a mutable version of the current actions dictionary
NSMutableDictionary *customActions=[NSMutableDictionary dictionaryWithDictionary:[theLayer actions]];

// add the new action for sublayers
[customActions setObject:[NSNull null] forKey:@"sublayers"];

// set theLayer actions to the updated dictionary 
theLayer.actions=customActions;

8、事务

 隐式事务

当图层树被没有获得事务的线程修改的时候将会自动创建隐式事务,当线程的运行循环(run-loop)执行下次迭代的时候将会自动提交事务。
重要 :当在一个没有运行循环(runloop)的线程修改图层的属性的时候,你必须使用显式的事务,所以UI处理都必须放主线程。子线程默认都没有runloop的

显式事务


临时禁用图层的action


你可以在修改图层属性值的时候通过设置事务的kCATransactionDisableActions值为YES来暂时禁用图层的行为。在事务范围所作的任何更改也不会因此而发生的动画。

代码2显示了一个示例,当把aLayer从可视化图层树移除的时候禁用淡出动画。
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
[aLayer removeFromSuperlayer];
[CATransaction commit];

你可以暂时改变响应改变图层属性的动画的时间,通过设置事务的kCATransactionAnimationDuration键的值为新的时间

代码4 中显示了一个嵌套两个事务的例子。最外层的事务设置隐式动画的时间为2秒,并设置图层的position属性值。内层的事务设置隐式动画的时间为5秒,并修改图层的opacity和zPosition属性值。
[CATransaction begin]; // outer transaction
 
// change the animation duration to 2 seconds
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                forKey:kCATransactionAnimationDuration];
// move the layer to a new position
theLayer.position = CGPointMake(0.0,0.0);
 
[CATransaction begin]; // inner transaction
// change the animation duration to 5 seconds
[CATransaction setValue:[NSNumber numberWithFloat:5.0f]
                 forKey:kCATransactionAnimationDuration];
 
// change the zPosition and opacity
theLayer.zPosition=200.0;
theLayer.opacity=0.0;
 
[CATransaction commit]; // inner transaction
[CATransaction commit]; // outer transaction


9、键-值编码兼容的容器类

CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值

为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。

+ (id)defaultValueForKey:(NSString *)key
{
    if ([key isEqualToString:@"masksToBounds"])
         return [NSNumber numberWithBool:YES];
 
    return [super defaultValueForKey:key]; 
}

Reproduced in: https: //my.oschina.net/dake/blog/196824

Guess you like

Origin blog.csdn.net/weixin_34211761/article/details/91586250