Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It can be easilly done by using Core Animation, animating a mask layer on the layer displaying the text.</p> <p>Try this in any plain UIViewController (you can start with a new Xcode project based on the <em>View-based application</em> project template), or grab my Xcode project <a href="http://dl.dropbox.com/u/713729/SlideToUnlock.zip" rel="nofollow noreferrer">here</a>:</p> <p><em>Note that the <code>CALayer.mask</code> property is only available in iPhone OS 3.0 and later.</em></p> <pre><code>- (void)viewDidLoad { self.view.layer.backgroundColor = [[UIColor blackColor] CGColor]; 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 = HUGE_VALF; maskAnim.duration = 1.0f; [maskLayer addAnimation:maskAnim forKey:@"slideAnim"]; textLayer.mask = maskLayer; [self.view.layer addSublayer:textLayer]; [super viewDidLoad]; } </code></pre> <p>The images used by this code are:</p> <p><a href="https://i.stack.imgur.com/kmq78.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kmq78.png" alt="Mask Layer"></a> <a href="https://i.stack.imgur.com/RFLIl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RFLIl.png" alt="Text Layer"></a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload