Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 设置UIImageView着色的动画_Iphone_Core Animation_Uiimageview - Fatal编程技术网

Iphone 设置UIImageView着色的动画

Iphone 设置UIImageView着色的动画,iphone,core-animation,uiimageview,Iphone,Core Animation,Uiimageview,类似于,我想对任意图像应用任意颜色的色调(aUIImageView)。但是,我希望色调在N秒的时间内逐渐消失,以产生“脉冲”效果。我会每米秒触发一次脉搏 虽然我认为我可以使用NSTimer来构建一个简单的解决方案来改变色调,但我认为我可能可以使用一些核心动画框架来获得一个更优雅(和高效)的解决方案。但是,我对核心动画不是很熟悉 通过核心动画可以创建这样的“脉冲”吗?如果是,如何使用?如果您计划使用核心动画,该方法将不同于您在上一个so问题的链接中演示的方法。相反,创建一个层,该层使用您所追求的颜

类似于,我想对任意图像应用任意颜色的色调(a
UIImageView
)。但是,我希望色调在N秒的时间内逐渐消失,以产生“脉冲”效果。我会每米秒触发一次脉搏

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


通过核心动画可以创建这样的“脉冲”吗?如果是,如何使用?

如果您计划使用核心动画,该方法将不同于您在上一个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的混合情况

致以最良好的祝愿


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

如果前面提到的任意图像不是矩形(如圆形),或者只是在其周围有一个空白边界(alpha为0的像素),这会起作用吗?因为如果你调用setBackgroundColor:,你将为那些不应该“点亮”的像素设置一种颜色。对,它将为整个矩形着色。不过,我认为对于其他核心图形方法也是如此。不过我不确定,因为我还没有试过。你可以在tintLayer中添加一个遮罩。我正在研究对UIImageView着色-tintLayer.mask=imageView.layer可能会起到作用。希望imageView.layer可以在两个位置使用。