zoukankan      html  css  js  c++  java
  • 使用动画改变UILabel的背景色

    使用动画改变UILabel的背景色


    当设置了UIView的backgroundColor,再去动画改变UILabel的背景色会失败

    //设置背景色
    label.backgroundColor = [UIColor redColor];
    
    ...
    
    //动画修改背景色
    CABasicAnimation* cocorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    CGColorRef originColor = [UIColor whiteColor].CGColor;
    CGColorRef darkGray = [UIColor blueColor].CGColor;
    
    cocorAnimation.fromValue = (__bridge id)originColor;
    cocorAnimation.toValue = (__bridge id)darkGray;
    
    cocorAnimation.duration = 0.8;
    [self.label.layer addAnimation:cocorAnimation forKey:@""];
    

    效果是 UILabel的背景色一点也没改变。。


    修改使用layer的backgroundColor

    //设置背景色
    label.layer.backgroundColor = [UIColor redColor].CGColor;
    
    ...
    
    //动画修改背景色
    CABasicAnimation* cocorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    CGColorRef originColor = [UIColor whiteColor].CGColor;
    CGColorRef darkGray = [UIColor blueColor].CGColor;
    
    cocorAnimation.fromValue = (__bridge id)originColor;
    cocorAnimation.toValue = (__bridge id)darkGray;
    
    cocorAnimation.duration = 0.8;
    [self.label.layer addAnimation:cocorAnimation forKey:@""];
    

    这次动画成功了。

    参考链接how-to-animate-the-background-color-of-a-uilabel

  • 相关阅读:
    洛谷P2568 GCD
    线段树(模板)
    题解 CF1296D 【Fight with Monsters】
    图片针对父元素居中 TileImg
    npm
    echarts线图,柱状图,饼图option
    mac下修改环境变量
    input获取焦点,但不调起键盘
    mac shh 关联git仓库
    根据滚动条触发动画
  • 原文地址:https://www.cnblogs.com/sunyanyan/p/5253047.html
Copyright © 2011-2022 走看看