创建颜色渐变view

    self.lzxview.frame = CGRectMake(20, 100, 300, 100);
    
    [self.view addSubview:self.lzxview];
    
    
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    // 开始颜色
    UIColor *startColor = [UIColor redColor];
    // 最终颜色
    UIColor *endColor = [UIColor blueColor];
    
    gradientLayer.colors = @[(__bridge id)startColor.CGColor,(__bridge id)endColor.CGColor];
    // 开始
    gradientLayer.startPoint = CGPointMake(0, 0);
    // 结束
    gradientLayer.endPoint = CGPointMake(1, 0);
    
    gradientLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.lzxview.frame), CGRectGetHeight(self.lzxview.frame));
    
    [self.lzxview.layer addSublayer:gradientLayer];
    
    /*
      默认为从上到下渐变
     
     (0,0)到(1,0)和(0,1)到(1,1)都是水平从左向右渐变;
     
     (0,0)到(1,1)是从左上角向右下角渐变;
     
     (0,1)到(1,0)室从左下角向右上角渐变。
     
     */


你可能感兴趣的:(创建颜色渐变view)