iPhone “slide to unlock” animation

前端 未结 13 827
感情败类
感情败类 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:28

    I took the best from above solutions and created a neat method that does all for you:

    - (void)createSlideToUnlockViewWithText:(NSString *)text
    {
        UILabel *label = [[UILabel alloc] init];
        label.text = text;
        [label sizeToFit];
        label.textColor = [UIColor whiteColor];
    
        //Create an image from the label
        UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, 0.0);
        [[label layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        CGFloat textWidth = textImage.size.width;
        CGFloat textHeight = textImage.size.height;
    
        CALayer *textLayer = [CALayer layer];
        textLayer.contents = (id)[textImage CGImage];
        textLayer.frame = CGRectMake(self.view.frame.size.width / 2 - textWidth / 2, self.view.frame.size.height / 2 - textHeight / 2, textWidth, textHeight);
    
        UIImage *maskImage = [UIImage imageNamed:@"Mask.png"];
        CALayer *maskLayer = [CALayer layer];
        maskLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.15] CGColor];
        maskLayer.contents = (id)maskImage.CGImage;
        maskLayer.contentsGravity = kCAGravityCenter;
        maskLayer.frame = CGRectMake(-textWidth - maskImage.size.width, 0.0, (textWidth * 2) + maskImage.size.width, textHeight);
    
        CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnimation.byValue = [NSNumber numberWithFloat:textWidth + maskImage.size.width];
        maskAnimation.repeatCount = HUGE_VALF;
        maskAnimation.duration = 2.0;
        maskAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        [maskLayer addAnimation:maskAnimation forKey:@"slideAnimation"];
    
        textLayer.mask = maskLayer;
        self.slideToUnlockLayer = textLayer;
        [self.view.layer addSublayer:self.slideToUnlockLayer];
    }
    

提交回复
热议问题