如何使UIView具有透明渐变动画的效果?不要使用计时器

时间:2017-04-27 06:20:32

标签: ios objective-c gpuimage cagradientlayer

gradientLayer = [CAGradientLayer layer];
        NSArray *colors = [NSArray arrayWithObjects:
                           (id)[[UIColor colorWithWhite:0 alpha:1.0] CGColor],  //clearColor,透明度为0(显示为黑色,就像黑洞.)
                           (id)[[UIColor colorWithWhite:0 alpha:0.5] CGColor],//clearColor,透明度为0.5
                           (id)[[UIColor colorWithWhite:0 alpha:0.0] CGColor],  //clearColor,透明度为1(显示为透明)
                           nil];
        [gradientLayer setColors:colors];
        [gradientLayer setStartPoint:CGPointMake(0, 0)];
        [gradientLayer setEndPoint:CGPointMake(1, 0)];
        [gradientLayer setFrame:_label.bounds];
       [_label.layer setMask:gradientLayer];

   [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updategradientLayer) userInfo:nil repeats:YES];

- (void)updategradientLayer
{
    static CGFloat test = - 0.1f;

    if (test >= 1.1)
    {
        test = - 0.1f;
        [CATransaction setDisableActions:YES];
        gradientLayer.locations  = @[@(test), @(test + 0.05), @(test + 0.1)];
    }
    else
    {
        [CATransaction setDisableActions:NO];
        gradientLayer.locations  = @[@(test), @(test + 0.05), @(test + 0.1)];
    }

    test += 0.1f;
}

0 个答案:

没有答案