IOS 设置颜色渐变和透明度渐变

项目中用到地图设置渐变色,查找资料找到两种方法:一种设置颜色,一种设置透明度;

//为颜色设置渐变效果:

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    CAGradientLayer *gradient = [CAGradientLayer layer];

//设置开始和结束位置(设置渐变的方向)

    gradient.startPoint = CGPointMake(0, 0);

    gradient.endPoint = CGPointMake(1, 0);

gradient.frame =CGRectMake(0,0,40,40);

gradient.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColorwhiteColor].CGColor,nil];

    [view.layer insertSublayer:gradient atIndex:0];

    [self.view addSubview:view];


    //为透明度设置渐变效果

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    UIColor *colorOne = [UIColor colorWithRed:(216/255.0)  green:(0/255.0)  blue:(18/255.0)  alpha:1.0];

    UIColor *colorTwo = [UIColor colorWithRed:(216/255.0)  green:(0/255.0)  blue:(18/255.0)  alpha:0.0];

    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];

CAGradientLayer *gradient = [CAGradientLayer layer];

//设置开始和结束位置(设置渐变的方向)

    gradient.startPoint = CGPointMake(0, 0);

    gradient.endPoint = CGPointMake(1, 0);

gradient.colors = colors;

    gradient.frame = CGRectMake(0, 0, 40, 40);

[view.layerinsertSublayer:gradientatIndex:0];

    [self.view addSubview:view];

你可能感兴趣的:(IOS 设置颜色渐变和透明度渐变)