代码之家  ›  专栏  ›  技术社区  ›  Shaggy Frog

设置UIImageView淡色的动画

  •  2
  • Shaggy Frog  · 技术社区  · 15 年前

    类似 this previous question UIImageView ). 不过,我希望色调在N秒内逐渐消失,以产生“脉冲”效果。我每分钟触发一次脉搏。

    NSTimer 为了改变色调,我想我可以使用一些核心动画框架来获得一个更加优雅(和高效)的解决方案。不过,我对核心动画不是很熟悉。

    在核心动画中创建这样的“脉冲”是可能的吗?如果是,怎么办?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Community CDub    8 年前

    如果您打算使用核心动画,那么方法将不同于您在前面的SO问题链接中演示的方法。相反,创建一个层,该层使用着色所需的颜色,然后为该层的不透明度设置动画。像这样:

    CALayer *tintLayer = [CALayer layer];
    
    // Center the layer on the view
    [tintLayer setBounds:[imageView bounds]];
    [tintLayer setPosition:CGPointMake([imageView bounds].size.width/2.0, 
                                       [imageView bounds].size.height/2.0)];
    
    // Fill the color
    [tintLayer setBackgroundColor:
               [[UIColor colorWithRed:0.5 green:0.5 blue:0.0 alpha:1.0] CGColor]];
    [tintLayer setOpacity:0.5];
    [[imageView layer] addSublayer:tintLayer];
    
    // Add the animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [animation setFromValue:[NSNumber numberWithFloat:0.5]];
    [animation setToValue:[NSNumber numberWithFloat:0.0]];
    // Animate back to the starting value automatically
    [animation setAutoreverses:YES];
    // Animate over the course of 5 seconds.
    [animation setDuration:5.0];
    
    [tintLayer addAnimation:animation forKey:@"opacity"];
    

    我唯一不确定的问题是,它是否也能正常工作,因为我们没有在染色层中指定混合模式。我将不得不看,看看什么混合是发生在默认与CA。

    更新:找到 another SO 这篇文章说,在CA中混合取决于图层的“不透明”属性。