【问题标题】:UILabel text animation with side to unlock effectUILabel 带有侧边解锁效果的文字动画
【发布时间】:2013-11-15 10:47:40
【问题描述】:

我想知道有人如何在不使用之前在这里询问的静态图像的情况下在 UILabel 的文本上实现“滑动解锁效果”。

// I'd like to use the uilable's current text to this sample code  but not seem to be able to do it.

    // --->  UIImage *textImage = [UIImage imageNamed:@"SlideToUnlock.png"];
      CGFloat textWidth = textImage.size.width;
      CGFloat textHeight = textImage.size.height;

  CALayer *textLayer = [CALayer layer];
  textLayer.contents = (id)[textImage CGImage];
  textLayer.frame = CGRectMake(10.0f, 215.0f, textWidth, textHeight);
  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(-textWidth, 0.0f, textWidth * 2, textHeight);

  // Animate the mask layer's horizontal position
  CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
  maskAnim.byValue = [NSNumber numberWithFloat:textWidth];
  maskAnim.repeatCount = 1e100f;
  maskAnim.duration = 1.0f;
  [maskLayer addAnimation:maskAnim forKey:@"slideAnim"];

  textLayer.mask = maskLayer;
  [self.view.layer addSublayer:textLayer];

谢谢

【问题讨论】:

  • 你会用什么?以编程方式生成的图像?你会完全使用不同的方法吗?您尝试解决的这种方法有什么问题?

标签: ios animation text uilabel calayer


【解决方案1】:

您可以使用以下项目。这是一个带有滑动解锁动画的UILabel

http://code4app.net/ios/Animated-Label/505fd71a6803fa1077000001

【讨论】:

  • 谢谢。这就是我要找的。但是提供的许可证有点严格。
【解决方案2】:
  • 顶部:具有不透明背景和清晰文本的 UILabel
    • 在drawRect中呈现清晰的文本:func通过复杂的屏蔽过程
  • 中间:执行重复动画的工作视图将图像移动到顶部标签后面
  • Bottom:按该顺序添加中间和顶部子视图的 UIView。可以是您希望文本的任何颜色

一个例子可以在这里看到https://github.com/jhurray/AnimatedLabelExample

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多