2009-12-15 76 views
1

this previous question类似,我想对任意图像应用任意颜色的色调(UIImageView)。但是,我希望色彩在N秒钟内消失,以产生“脉冲”效果。我会每M秒触发一次脉冲。动画UIImageView色调

虽然我认为我可以使用NSTimer来改变颜色,但我认为我可以使用一些Core Animation框架来创建更加优雅(高效)的解决方案。不过,我对Core Animation不是很熟悉。

正在用Core Animation创建这样的“脉冲”吗?如果是这样,怎么样?

回答

1

如果您打算使用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进行的混合。

此致敬礼。

更新:找到another SO后,说在CA混合取决于该层的'不透明'属性。

+0

如果上述任意图像不是矩形(如圆圈),或者它周围有空白边框(带有0 alpha的像素),这样做会工作吗?因为如果你打电话给setBackgroundColor :,你会为那些不应该被“点亮”的像素设置颜色。 – 2009-12-16 18:13:20

+0

好吧,它会着色整个矩形。不过,我认为这也适用于其他Core Graphics方法。但我不确定,因为我没有尝试过。 – 2009-12-16 18:43:17

+0

你可以添加一个掩码到tintLayer。我正在着色UIImageView - tintLayer.mask = imageView.layer可能会做到这一点。希望imageView.layer喜欢在两个地方使用。 – 2013-05-23 06:41:36