Color, 霓虹灯效果练习

#import "RootViewController.h"

@interface RootViewController (){

    CAGradientLayer *gradientLayer;

    NSTimer *timer;

}

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage   imageNamed:@"美女图.jpg"]];

    imageView.frame = self.view.bounds;

    gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = imageView.bounds;

    [imageView.layer addSublayer:gradientLayer];

    [self.view addSubview:imageView];

    [imageView release];

    //设置颜色渐变方向

    gradientLayer.startPoint = CGPointMake(0, 0);

    gradientLayer.endPoint = CGPointMake(0, 1);

    //设定颜色组

    gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor purpleColor].CGColor];//((__bridge id)把颜色转化为一个对象)

    //设定颜色分割点

    gradientLayer.locations = @[@(0.5f), @(1.0f)];

    //设置时间定时器

    timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(TimeChange) userInfo:nil repeats:YES];

    

    

}

- (void)TimeChange{

    //定时随机颜色

    gradientLayer.colors = @[(id)[[UIColor clearColor] CGColor], (id)[UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0].CGColor];

    gradientLayer.locations = @[@(arc4random() % 10 / 10.0f), @(1.0f)];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/hsxblog/p/4927362.html