iPhone “slide to unlock” animation

前端 未结 13 796
感情败类
感情败类 2020-11-28 00:49

Any ideas as to how Apple implemented the \"slide to unlock\" (also, \"slide to power off\" is another identical example) animation?

I thought about some sort of ani

13条回答
  •  一生所求
    2020-11-28 01:26

    I added the code provided above by Pascal as a category on UILabel so you can animate any UILabel in this fashion. Here's the code. Some params might need to be changed for your background colors, etc. It uses the same mask image that Pascal has embedded in his answer.

    //UILabel+FSHighlightAnimationAdditions.m
    #import "UILabel+FSHighlightAnimationAdditions.h"
    #import 
    #import 
    
    @implementation UILabel (FSHighlightAnimationAdditions)
    
    - (void)setTextWithChangeAnimation:(NSString*)text
    {
        NSLog(@"value changing");
        self.text = text;
        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(self.frame.size.width * -1, 0.0f, self.frame.size.width * 2, self.frame.size.height);
    
        // Animate the mask layer's horizontal position
        CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnim.byValue = [NSNumber numberWithFloat:self.frame.size.width];
        maskAnim.repeatCount = 1e100f;
        maskAnim.duration = 2.0f;
        [maskLayer addAnimation:maskAnim forKey:@"slideAnim"];
    
        self.layer.mask = maskLayer;
    }
    
    @end
    
    //UILabel+FSHighlightAnimationAdditions.h
    #import 
    @interface UILabel (FSHighlightAnimationAdditions)
    
    - (void)setTextWithChangeAnimation:(NSString*)text;
    
    @end
    

提交回复
热议问题