iOS White to Transparent Gradient Layer is Gray

前端 未结 7 1226
清歌不尽
清歌不尽 2021-02-01 11:53

I have a CAGradientLayer inserted to the bottom of this small detail view that pops up at the bottom of the app. As you can see, I\'ve set the colors from white to clear, but th

7条回答
  •  感情败类
    2021-02-01 12:26

    Worth pointing out that any other colour will work like this... using a combination of the two answers above....

    Objective C

    UIColor *colour = [UIColor redColor];
    NSArray *colourArray = @[(id)[colour colorWithAlphaComponent:0.0f].CGColor,(id)colour.CGColor]
    NSArray *locations = @[@0.2,@0.8];
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = colourArray;
    gradientLayer.locations = locations;
    gradientLayer.frame = self.frame;
    
    [self.layer addSublayer:gradientLayer];
    

    Swift 3

    let colour:UIColor = .red
    let colours:[CGColor] = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
    let locations:[NSNumber] = [0.2,0.8]
    
    let gradientLayer = CAGradientLayer()
    gradientLayer.colors = colours
    gradientLayer.locations = locations
    gradientLayer.frame = frame
    
    layer.addSublayer(gradientLayer)
    

提交回复
热议问题