CABasicAnimation动画--lable字体大小改变

CABasicAnimation动画--lable字体大小改变

开发过程中遇到一个改变字体大小的动画需求

研究良久  发现可以通过CABasicAnimation 和 CAAnimationGroup简单实现

CABasicAnimation有三个property   fromValue  toValue  ByValue

创建CABasicAnimation 时,你需要通过-setFromValue 和-setToValue 来指定一个开始值和结束值。 当你增加基础动画到层中的时候,它开始运行。当用属性做动画完成时,例如用位置属性做动画,层就会立刻 返回到它的初始位置

在做动画时,我们希望将几种动画叠加起来同时播放,或者依次连续播放。

这时候使用 组合CAAnimationGroup

以下是实现代码,

#import "ViewController.h"

@interface ViewController ()
{
    UILabel *lable;
    UIButton *button;
    UIView *vview;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 40);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    vview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 80)];
    vview.backgroundColor = [UIColor greenColor];
    vview.center = self.view.center;
    vview.layer.cornerRadius = vview.frame.size.height/2;
    [self.view addSubview:vview];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 80)];
    lable.text = @"动画已经准备好";
    lable.font = [UIFont systemFontOfSize:30];
    lable.layer.borderWidth = 3;
    lable.layer.borderColor = [UIColor whiteColor].CGColor;
    lable.textAlignment = NSTextAlignmentCenter;
    lable.layer.cornerRadius = lable.frame.size.height/2;
    [vview addSubview:lable];
    
    
//    lable.hidden = YES;
}

- (void)buttonClick
{
    [UIView animateWithDuration:3 animations:^{
        
        CABasicAnimation * aniScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        aniScale.fromValue = [NSNumber numberWithFloat:0.5];
        aniScale.toValue = [NSNumber numberWithFloat:4.0];
        aniScale.duration = 3;
        aniScale.delegate = self;
        aniScale.removedOnCompletion = NO;
        aniScale.repeatCount = 1;
        [vview.layer addAnimation:aniScale forKey:@"babyCoin_scale"];
        
    } completion:^(BOOL finished) {
        [self babyCoinFadeAway];
    }];
}

-(void)babyCoinFadeAway
{

    //相当与两个动画  合成
    //位置改变
    CABasicAnimation * aniMove = [CABasicAnimation animationWithKeyPath:@"position"];
    aniMove.fromValue = [NSValue valueWithCGPoint:vview.layer.position];
    aniMove.toValue = [NSValue valueWithCGPoint:CGPointMake(500, 300)];
    //大小改变
    CABasicAnimation * aniScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    aniScale.fromValue = [NSNumber numberWithFloat:3.0];
    aniScale.toValue = [NSNumber numberWithFloat:0.5];
    
    CAAnimationGroup * aniGroup = [CAAnimationGroup animation];
    aniGroup.duration = 3.0;//设置动画持续时间
    aniGroup.repeatCount = 1;//设置动画执行次数
    aniGroup.delegate = self;
    aniGroup.animations = @[aniMove,aniScale];
    aniGroup.removedOnCompletion = NO;
    aniGroup.fillMode = kCAFillModeForwards;  //防止动画结束后回到原位
//    [lable.layer removeAllAnimations];
    [vview.layer addAnimation:aniGroup forKey:@"aniMove_aniScale_groupAnimation"];
    
}

@end


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值