我不确定之前是否有人提出过类似问题,但我很难找到相关信息。也许我没有使用正确的搜索术语,如果已经存在答案,希望有人能够指引我正确的方向,非常感谢!
我刚刚注意到,在iOS 7.1更新中,“滑动以解锁”文本的闪烁动画已经发生了变化。现在聚光灯呈椭圆形/菱形状,沿着字母连续播放,而不会在它后面的视图上显示。
过去,我曾通过逐个更改字母的颜色来复制此类特性,但对于这个问题,动画是穿过字母的中间,而不影响背景的。
我该如何复制这种效果?
我不确定之前是否有人提出过类似问题,但我很难找到相关信息。也许我没有使用正确的搜索术语,如果已经存在答案,希望有人能够指引我正确的方向,非常感谢!
我刚刚注意到,在iOS 7.1更新中,“滑动以解锁”文本的闪烁动画已经发生了变化。现在聚光灯呈椭圆形/菱形状,沿着字母连续播放,而不会在它后面的视图上显示。
过去,我曾通过逐个更改字母的颜色来复制此类特性,但对于这个问题,动画是穿过字母的中间,而不影响背景的。
我该如何复制这种效果?
CALayer *maskLayer = [CALayer layer];
// Mask image ends with 0.15 opacity on both sides. Set the background color of the layer
// to the same value so the layer can extend the mask image.
maskLayer.backgroundColor = [[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.15f] CGColor];
maskLayer.contents = (id)[[UIImage imageNamed:@"Mask.png"] CGImage];
// Center the mask image on twice the width of the text layer, so it starts to the left
// of the text layer and moves to its right when we translate it by width.
maskLayer.contentsGravity = kCAGravityCenter;
maskLayer.frame = CGRectMake(myLabel.frame.size.width * -1, 0.0f, myLabel.frame.size.width * 2, myLabel.frame.size.height);
// Animate the mask layer's horizontal position
CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
maskAnim.byValue = [NSNumber numberWithFloat:myLabel.frame.size.width];
maskAnim.repeatCount = 1e100f;
maskAnim.duration = 1.5f;
[maskLayer addAnimation:maskAnim forKey:@"slideAnim"];
myLabel.layer.mask = maskLayer;
顶部: UILabel,具有不透明的背景和清晰的文本
中间: Worker View,执行重复动画将图像移动到顶部标签后面
底部: UIView,您按照该顺序添加中间和顶部子视图。可以是任何您想要的文本颜色
+colorWithPatternImage
或 -initWithPatternImage
初始化的 UIColor,使用动画图像并将标签的背景色设置为透明。我没有尝试过这个,但我认为它应该可以工作。我认为这是一个半透明视图,但它是一个特殊的视图,在其中覆盖了drawrect以使用与其下方视图中像素相同的颜色(但更强烈以使其可见)来着色每个字母的每个像素。 可以将其想象为放大镜视图。它显示了其下方视图的放大版本。