繁体   English   中英

动画 UIImageView 色调

[英]Animating a UIImageView tint

与上一个问题类似,我想将任意颜色的色调应用于任意图像( UIImageView )。 但是,我希望色调在 N 秒后逐渐消失,以产生“脉冲”效果。 我将每 M 秒触发一次脉冲。

虽然我认为我可以使用NSTimer构建一个简单的解决方案来改变色调,但我认为我可能可以使用一些 Core Animation 框架来获得更优雅(和高效)的解决方案。 但是,我对 Core Animation 不是很熟悉。

使用 Core Animation 是否可以创建这样的“脉冲”? 如果是这样,如何?

如果您计划使用核心动画,则该方法将与您之前的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进行的混合。

最好的祝福。

更新:找到另一个SO帖子,说明在CA中混合取决于图层的“不透明”属性。

聚会迟到了,但我找到了动画UIImageViewtintColor 您只需要提供contentsMultiplyColor作为CABasicAnimation keyPath 例子:

// 5 seconds infinite pulse animation from current tintColor to red color
let basicAnimation = CABasicAnimation(keyPath: "contentsMultiplyColor")
basicAnimation.duration = 5.0 
basicAnimation.fromValue = imageView.tintColor.cgColor
basicAnimation.toValue = UIColor.red.cgColor
basicAnimation.autoreverses = true
basicAnimation.repeatCount = Float.infinity

imageView.layer.add(basicAnimation, forKey: "TintColorAnimationKey")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM