iOS

隐式动画

2015-11-12  本文已影响166人  iOS_成才录

一、前言

二、什么是隐式动画?

三、可动画属性 Animatable Properties


// 查看部分头文件,还有其他的可动画属性不在此

/* The bounds of the layer. Defaults to CGRectZero. Animatable. */
@property CGRect bounds;

/* The position in the superlayer that the anchor point of the layer's
 * bounds rect is aligned to. Defaults to the zero point. Animatable. */
@property CGPoint position;

/* The Z component of the layer's position in its superlayer. Defaults
 * to zero. Animatable. */
@property CGFloat zPosition;

/* Defines the anchor point of the layer's bounds rect, as a point in
 * normalized layer coordinates - '(0, 0)' is the bottom left corner of
 * the bounds rect, '(1, 1)' is the top right corner. Defaults to
 * '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */
@property CGPoint anchorPoint;

/* The Z component of the layer's anchor point (i.e. reference point for
 * position and transform). Defaults to zero. Animatable. */
@property CGFloat anchorPointZ;

/* A transform applied to the layer relative to the anchor point of its
 * bounds rect. Defaults to the identity transform. Animatable. */
@property CATransform3D transform;

/* Convenience methods for accessing the `transform' property as an
 * affine transform. */
- (CGAffineTransform)affineTransform;
- (void)setAffineTransform:(CGAffineTransform)m;

/* Unlike NSView, each Layer in the hierarchy has an implicit frame
 * rectangle, a function of the `position', `bounds', `anchorPoint',
 * and `transform' properties. When setting the frame the `position'
 * and `bounds.size' are changed to match the given frame. */
@property CGRect frame;

/* When true the layer and its sublayers are not displayed. Defaults to
 * NO. Animatable. */
@property(getter=isHidden) BOOL hidden;

四 隐式动画 实现 : CATransaction(动画事务)

[CATransactionbegin];
[CATransactionsetDisableActions:YES];
self.myview.layer.position= CGPointMake(10,10);
[CATransactioncommit];

五、实例

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CALayer *greenLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加 一个自定义的layer到控制器的view的layer上
    CALayer *layer = [CALayer layer];
    
    _greenLayer = layer;
    
    layer.position = CGPointMake(0, 0);
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.anchorPoint = CGPointMake(0, 0);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    
    [self.view.layer addSublayer:layer];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 开启事务
    [CATransaction begin];
    
    
//    [CATransaction setDisableActions:YES];
//    [CATransaction setAnimationDuration:2];
    _greenLayer.position = CGPointMake(arc4random_uniform(250), arc4random_uniform(300));
    
    _greenLayer.backgroundColor = [self randomColor].CGColor;
    _greenLayer.cornerRadius = arc4random_uniform(50);
    
    _greenLayer.borderColor = [self randomColor].CGColor;
    _greenLayer.borderWidth = arc4random_uniform(5);
    
    // 提交事务
    [CATransaction commit];
    
}

/**
 *  返回 随机色
 */
- (UIColor *)randomColor
{
    CGFloat r = arc4random_uniform(256) / 255.0;
    CGFloat g = arc4random_uniform(256) / 255.0;
    CGFloat b = arc4random_uniform(256) / 255.0;
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

@end
     ```



上一篇 下一篇

猜你喜欢

热点阅读