我正在尝试将alpha渐变应用于UIView,但根本无法使其正常工作。我当前的drawRect:方法看起来像这样-
- (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,将颜色空间更改为灰度等。
向我指出正确方向的任何帮助将不胜感激。谢谢
最佳答案
通过确保将视图的背景色设置为透明,然后在图像视图上应用视图的图层作为遮罩,我解决了该问题。
关于ios - 无法让CGGradientRef与Alpha一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22258279/