iOS-核心动画(二)CABasicAnimation

CABasicAnimation

CABasicAnimation:基础动画

概念:

通过改变某个属性的值来产生动画。只能设置两个值。

重要属性:

  • fromValue : keyPath对应的初始值 ,如果不设置不会反回到起始位置;
  • toValue : keyPath对应的结束值;
  • byValue:通过哪个值;

演示示例

基础动画主要提供了对于CALayer对象中的可变属性进行简单动画的操作。比如:位移、透明度、缩放、旋转、背景色等等。下面简单的演示两个功能吧。

位移动画演示

#import "ViewController.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

@interface ViewController ()

{
     UIView *demoView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    demoView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-100,100 ,100 )];
    demoView.backgroundColor = [UIColor redColor];
    [self.view addSubview:demoView];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self demo1:[[touches anyObject] locationInView:self.view]];
}

//移动中心点
- (void)demo1:(CGPoint)toValue{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
    animation.toValue = [NSValue valueWithCGPoint:toValue];

    // 动画的持续时间
    animation.duration = 3;
    // 动画执行的总时间受动画速度的影响
    // animation.speed = 5;

    // 设置动画在完成的时候固定在完成的状态,这个属性必须把removeOnCompletion设置成NO,这个属性才能使用
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;

    // 控制速度
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    // CALayer -> addanimation:添加动画
    // addAnimation:动画
    // forKey:表示动画的字符串,可以通过key来找到这个动画
   [demoView.layer addAnimation:animation forKey:@"positionAnimation"];

    // 找到这个动画的方式
    // CABasicAnimation *an = (CABasicAnimation *)[self.petalLayer animationForKey:@"可以这个通过key来找到这个动画"];

    // 封装
    /* - (CABasicAnimation *)moveAnimation{ if (_moveAnimation) { return _moveAnimation; } _moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; // CGPoint -> 转id // CGPoint -> NSValue _moveAnimation.fromValue = [NSValue valueWithCGPoint:self.petalLayer.position]; _moveAnimation.toValue = [NSValue valueWithCGPoint:toValue]; return _moveAnimation; } */

}

效果图:

放大缩小动画演示

#import "ViewController.h"

@interface ViewController ()

//背景
@property (nonatomic, strong)CALayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view.layer addSublayer:self.layer];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self demo2];
}
//心跳
- (void)demo2{
    self.view.backgroundColor = [UIColor whiteColor];

    UIImage *image = [UIImage imageNamed:@"心跳"];
    self.layer.contents = (id)image.CGImage;
    self.layer.bounds = CGRectMake(0, 0, image.size.width/10, image.size.height/10);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    /** * 放大后还原到原来的位置,以动画的方法 * 先慢后快 * 一直循环 */
    animation.fromValue = [NSValue valueWithCGRect:self.layer.bounds];
    animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, image.size.width/7, image.size.height/7)];
    animation.repeatCount = HUGE;
    animation.duration = 0.5;
    //    以动画效果还原开始的状态
    animation.autoreverses = YES;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self.layer addAnimation:animation forKey:@""];
}

- (CALayer *)layer{
    if (_layer) {
        return _layer;
    }

    _layer = [CALayer layer];
    _layer.backgroundColor = [UIColor redColor].CGColor;
    _layer.position = CGPointMake(self.view.center.x, self.view.center.y+100);
    UIImage *image = [UIImage imageNamed:@"background"];
    _layer.bounds = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
    _layer.contents = (id)image.CGImage;

    return _layer;
}

放大缩小效果图:
iOS-核心动画(二)CABasicAnimation_第1张图片

你可能感兴趣的:(动画,CAProperty,ios基础动画)