iOS 7.1 Slide To Unlock Text Animation

自作多情 提交于 2019-12-03 02:57:07
Hitesh Vaghela

You can animate label text and use custom slider for it, I hope it helps you:

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;

You should be able to use the mask property of CALayer to create a cutout of the contents of another layer.

Set the mask to contain your text (maybe a CATextLayer can work here). This is what Shimmer says it uses.

Make the foreground color of your label be a UIColor initiated with

+colorWithPatternImage or

-initWithPatternImage

using an animated image and setting the background color of the label to transparent. I've not tried this, but I don't see why it wouldn't work.

The best way to do this is with a multi layer object.

  • Top: UILabel with opaque background and clear text

    • Clear text is rendered in drawRect: func through complicated masking process
  • Middle: Worker View that is performing a repeating animation moving an image behind the top label

  • Bottom: a UIView that you add the middle and top subview to in that order. Can be whatever color you want the text to be

An example can be seen here https://github.com/jhurray/AnimatedLabelExample

The most effective way I've found to recreate the glimmering text effect is to use the Shimmer Cocoapod created by Facebook. Below is the example image from the Shimmer GitHub repo, which is located at the following URL: https://github.com/facebook/Shimmer

Shimmer example

There are full instructions to install and use Shimmer on the repo, but the gist is that after installing the Cocoapod you'll add a special subview or layer into which will go the contents you wish to have glimmer/shimmer, then set the effect to start.

Try to have a semi-transparent foreground with transparent cutouts for the letters. The "glimmer" can be moved across behind the cutouts.

Make a layer on top that has cutout layers with an animated PNG or something as the background.

Under this layer, have another layer with exactly the reverse transparency (letters are opaque and space between letters is transparent.

This way, the user sees through the letters to the animation, and between the letters to whatever the letters are over.

Just make sure you have code to keep the layers in the right order.

I think that it's a semi transparent view, but it's a special view in which the drawrect is overridden to color each pixel of the letters with the same color (but stronger to make it visible) of the pixel in the view beneath it. Imagine this like the magnifying view. it displays a magnified version of the the view beneath it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!