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

本文介绍如何使用CABasicAnimation和CAAnimationGroup实现iOS应用中字体大小的平滑变化动画效果,包括创建动画、设置动画参数及组合多个动画。

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


<think> 我们正在使用Element UI,设置多个el-label的字体大小。可以通过以下方法实现: 1. 使用全局样式(修改所有el-label) 2. 使用局部样式(通过自定义类名) 3. 内联样式(单个设置) 注意:Element UI的el-label组件实际上对应的是<label>元素,但有时可能被包裹在其他元素中。我们通常通过CSS选择器来设置。 下面分别展示几种方法: </think> ### 设置多个 `el-label` 字体大小方案 在 Element UI 中为多个 `<el-label>` 设置字体大小,可通过以下方法实现: #### 方案1:全局样式覆盖(推荐批量修改) ```css /* 在全局CSS中 */ .el-form-item__label { font-size: 16px !important; /* 默认标签 */ } .special-label .el-form-item__label { font-size: 18px !important; /* 特定表单组 */ } ``` ```vue <template> <!-- 所有标签受影响 --> <el-form> <el-form-item label="用户名">...</el-form-item> </el-form> <!-- 特定组使用大字号 --> <el-form class="special-label"> <el-form-item label="地址">...</el-form-item> </el-form> </template> ``` #### 方案2:局部样式穿透(SCSS/SASS) ```vue <template> <el-form> <!-- 多个label使用相同样式 --> <el-form-item :class="['label-large', 'label-bold']" label="姓名">...</el-form-item> <el-form-item class="label-large" label="电话">...</el-form-item> </el-form> </template> <style lang="scss" scoped> /* 穿透Element UI样式 */ :deep(.label-large) .el-form-item__label { font-size: 18px; } :deep(.label-bold) .el-form-item__label { font-weight: bold; } </style> ``` #### 方案3:行内样式(快速调试) ```vue <template> <el-form> <el-form-item :label="item.name" v-for="item in labelList" :key="item.id" :style="{ fontSize: item.size + 'px' }" <!-- 动态设置 --> > ... </el-form-item> </el-form> </template> <script> export default { data() { return { labelList: [ { id:1, name:"邮箱", size:16 }, { id:2, name:"紧急联系人", size:18 } ] } } } </script> ``` #### 方案4:CSS变量统一管理 ```css :root { --label-primary: 14px; --label-secondary: 12px; } .el-form-item__label { font-size: var(--label-primary); } .compact-form .el-form-item__label { font-size: var(--label-secondary); } ``` ### 注意事项 1. **样式优先级** - 行内样式 > ID选择器 > 类选择器 > 标签选择器 - 使用 `!important` 需谨慎,推荐用层级覆盖 2. **响应式适配** ```css /* 根据屏幕尺寸调整 */ @media (max-width: 768px) { .el-form-item__label { font-size: 14px !important; } } ``` 3. **动态类名技巧** ```vue <el-form-item :class="['form-label', { 'urgent-text': item.isUrgent }]" v-for="item in items" > ``` ```css .urgent-text .el-form-item__label { font-size: 18px; color: #f56c6c; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值