CAGradientLayer使用

建一个UIview的子类GradientView。在子类的GradientView.m文件中重写

+ (Class)layerClass {

    return [CAGradientLayer class];

}


- (void)didMoveToSuperview {    

    self.backgroundColor = [UIColor blackColor];

    CGColorRef color = [UIColor blackColor].CGColor;

    UIColor *color1 = [UIColor colorWithRed:0.01 green:0.20 blue:0.80 

                                                                alpha:1.0];

    UIColor *color2 = [UIColor colorWithRed:1.00 green:0.50 blue:0.00 

                                                                alpha:1.0];

    UIColor *color3 = [UIColor colorWithRed:0.35 green:0.74 blue:0.11 

                                                                alpha:1.0];

    NSArray *colors = [NSArray arrayWithObjects:(id)[color1 CGColor],

                       [color2 CGColor],

                       [color3 CGColor],

                       nil];


    CAGradientLayer *gLayer = (CAGradientLayer *)self.layer;

    gLayer.colors = colors;

    gLayer.locations = [NSArray arrayWithObjects:

                        [NSNumber numberWithFloat:0.0], 

                        [NSNumber numberWithFloat:0.4], 

                        [NSNumber numberWithFloat:0.9], 

                        nil];

    gLayer.startPoint = CGPointMake(0.5, 0);

    gLayer.endPoint = CGPointMake(0.51);


    

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:

                                                            @"startPoint"];

    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.5, 1)];

    anim.duration = 6;

    anim.timingFunction = [CAMediaTimingFunction 

                           functionWithName:kCAMediaTimingFunctionEaseOut];

    [gLayer addAnimation:anim forKey:@"start"];

                        

   

    anim = [CABasicAnimation animationWithKeyPath:@"colors"];

    anim.fromValue = [NSArray arrayWithObjects:(id)color, color, color, nil];

    anim.duration = 6;

    anim.timingFunction = [CAMediaTimingFunction 

                           functionWithName:kCAMediaTimingFunctionEaseOut];

    [gLayer addAnimation:anim forKey:@"colors"];

}


可以在UIviewcontroller的view中添加这个GradientView看效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值