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


阅读更多
个人分类: IOS开发
想对作者说点什么? 我来说一句

iOS 文字动画(每个字逐个显示)

2016年01月16日 63KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭