Ios Can';不要让CGGradientRef与alpha一起工作
我试图将alpha渐变应用于UIView,但根本无法使其工作。我当前的drawRect:方法如下所示-Ios Can';不要让CGGradientRef与alpha一起工作,ios,core-graphics,Ios,Core Graphics,我试图将alpha渐变应用于UIView,但根本无法使其工作。我当前的drawRect:方法如下所示- - (void)drawRect:(CGRect)rect { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGGradientRef alphaGradient; CGColorSpaceRef rgbColorSpace; size_t num_locations = 2;
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef alphaGradient;
CGColorSpaceRef rgbColorSpace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0, // Start color
1.0, 1.0, 1.0, 0.0 }; // End color
rgbColorSpace = CGColorSpaceCreateDeviceRGB();
alphaGradient = CGGradientCreateWithColorComponents(rgbColorSpace, components, locations, num_locations);
CGRect currentBounds = self.bounds;
CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
CGContextDrawLinearGradient(currentContext, alphaGradient, midCenter, bottomCenter, 0);
CGGradientRelease(alphaGradient);
CGColorSpaceRelease(rgbColorSpace);
}
我确信我在这里遗漏了一些东西。我尝试将RGB值更改为0.0而不是1.0,将颜色空间更改为灰度,等等
如果能帮我指明正确的方向,我将不胜感激。谢谢我使用CAGradient:
UIView * viewToGradient = // view to add gradient
UIColor * color1 = [UIColor lightGrayColor]; // gradient color 1
UIColor * color2 = [UIColor clearColor]; // gradient color 2
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = viewToGradient.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil];
gradientLayer.opacity = .6;
[viewToGradient.layer addSublayer:gradientLayer];
我通过确保视图的背景色设置为“清除”,然后将视图的图层作为遮罩应用到图像视图上,解决了这个问题。我使用的是CAGradientLayer,但遇到的问题是它看起来不像我想要的那样平滑。不过,如果你不需要CGGradients的平滑度,那么它确实工作得很好。你试过提高分辨率吗?[gradientLayer设置内容缩放:[UIScreen mainScreen]。缩放];