如何制作一个秘密的iOS应用程序文本animation

我试图复制秘密应用程序的文本标签转换。 有没有人最好的方法来处理它?

看起来他们每个字母都以清晰的文字颜色开始,然后将其设置为灰色,然后将其设置为白色文字颜色。

以下是一些截图: 在这里输入图像说明在这里输入图像说明在这里输入图像说明在这里输入图像说明

这是另一个解决schemehttps://github.com/zipme/RQShineLabel

我使用CADisplayLink和NSAttributedString一起,这样我们只需要一个UILabel,看看:)

感谢大家的帮助。 我能够通过一些修改来保持标签的淡入淡出。 这里是我的完整源代码: https : //github.com/NatashaTheRobot/SecretTextAnimationExample

这是一个可能的方法。

首先,让我们注意到一个UILabel可以保存并显示一个NSAttributedString。 使用NSMutableAttributedString,你可以使每个字母不同的颜色。

所以,从两个标签开始,一个在另一个的顶部(即在它的前面,隐藏它),具有相同的文本但是不同的字母着色。 现在,将顶端的阿尔法淡化为零,从而逐渐揭示其背后的一个。 因此,每封信似乎将逐渐呈现出背后字母的颜色。

我只是真的要扩展@matt的一个快速的例子来说明如何做到这一点。 您从两个标签开始,一个直接在另一个上面,具有相同的属性和alignment。 在两个标签都configuration好之后,您就可以开始制作animation了,您只需将顶部标签淡出即可。

- (void)awakeFromNib { [super awakeFromNib]; [self.view setBackgroundColor:[UIColor blackColor]]; NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 200.0)]; [label1 setNumberOfLines:0]; [label1 setBackgroundColor:[UIColor clearColor]]; [label1 setAttributedText:[self randomlyFadedAttStringFromString:text]]; [self.view addSubview:label1]; UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 200.0)]; [label2 setNumberOfLines:0]; [label2 setBackgroundColor:[UIColor clearColor]]; [label2 setTextColor:[UIColor whiteColor]]; [label2 setAttributedText:[[NSAttributedString alloc] initWithString:text]]; [self.view addSubview:label2]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ [label2 setAlpha:0.0]; } completion:^(BOOL finished) { [label2 removeFromSuperview]; }]; }); } 

然后为底部标签创build一个特殊的属性string。 此属性string不应该修改您在NSForegroundColorAttributeName属性以外的其他标签上设置的NSForegroundColorAttributeName属性。 你可能想也可能不想想出某种algorithm,以确定哪些字母应该以什么量消失,但是下面的代码将会从inputstring生成一个属性string,其中每个字母alpha只是一个0之间的随机值和1。

 - (NSAttributedString *)randomlyFadedAttStringFromString:(NSString *)string { NSMutableAttributedString *outString = [[NSMutableAttributedString alloc] initWithString:string]; for (NSUInteger i = 0; i < string.length; i ++) { UIColor *color = [UIColor colorWithWhite:1.0 alpha:arc4random_uniform(100) / 100.0]; [outString addAttribute:NSForegroundColorAttributeName value:(id)color range:NSMakeRange(i, 1)]; } return [outString copy]; } 
Interesting Posts