iOS梯度动画

作者: 微末凡尘_ | 来源:发表于2016-11-20 13:19 被阅读237次

    效果图

    PJAnimatedMaskLabel.gif

    代码

    设置梯度条

    - (void)didMoveToWindow {
        
        [self.layer addSublayer:_gradientLayer];
        
        CABasicAnimation *gradientAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
        gradientAnimation.fromValue = @[@0.0, @0.0, @0.25];
        gradientAnimation.toValue = @[@0.75, @1.0, @1.0];
        gradientAnimation.duration = 3.0;
        gradientAnimation.repeatCount = CGFLOAT_MAX;
        [_gradientLayer addAnimation:gradientAnimation forKey:nil];
    }
    
    - (void)setupUI {
        
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.colors = @[(id)[UIColor blackColor].CGColor,
                                  (id)[UIColor whiteColor].CGColor,
                                  (id)[UIColor blackColor].CGColor];
        _gradientLayer.locations = @[@0.25, @0.5, @0.75];
        _gradientLayer.startPoint = CGPointMake(0.0, 0.5);
        _gradientLayer.endPoint = CGPointMake(1.0, 0.5);
    }
    
    MaskLabel.gif

    设置显示区域

    - (void)layoutSubviews {
        
        [super layoutSubviews];
        _gradientLayer.frame = CGRectMake(-self.bounds.size.width,
                                          self.bounds.origin.y,
                                          3 * self.bounds.size.width,
                                          self.bounds.size.height);
    }
    

    设置文字

    - (void)setText:(NSString *)text withFontSize:(CGFloat)size {
        
        [self setNeedsDisplay];
        
        NSMutableDictionary *textAttributes = [NSMutableDictionary dictionary];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:size];
        
        textAttributes[NSParagraphStyleAttributeName] = style;
        textAttributes[NSFontAttributeName] = font;
        
        UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0);
        [text drawInRect:self.bounds withAttributes:textAttributes];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        CALayer *maskLayer = [CALayer layer];
        maskLayer.backgroundColor = [UIColor clearColor].CGColor;
        maskLayer.frame = CGRectOffset(self.bounds, self.bounds.size.width, 0);
        maskLayer.contents = (id)(image.CGImage);
        _gradientLayer.mask = maskLayer;
    }
    

    Demo地址

    https://github.com/codelyw/iOSDemo

    参考

    iOS Animations by Tutorials

    相关文章

      网友评论

        本文标题:iOS梯度动画

        本文链接:https://www.haomeiwen.com/subject/yefypttx.html