【发布时间】:2010-10-25 02:07:09
【问题描述】:
有没有办法让 UIView 的背景变成渐变而不继承它?我也不想使用图像文件来完成此操作。仅仅为了为背景绘制渐变就必须继承 UIView 似乎很迟钝。
【问题讨论】:
有没有办法让 UIView 的背景变成渐变而不继承它?我也不想使用图像文件来完成此操作。仅仅为了为背景绘制渐变就必须继承 UIView 似乎很迟钝。
【问题讨论】:
您可以使用+[UIColor colorWithPatternImage:] 生成带图案的背景。示例(带上你自己的 CGGradient):
// Allocate color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Allocate bitmap context
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, 320, 480, 8, 4 * 320, colorSpace, kCGImageAlphaNoneSkipFirst);
//allocate myGradient
CGFloat locationList[] = {0.0f, 1.0f};
CGFloat colorList[] = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f};
CGGradientRef myGradient = CGGradientCreateWithColorComponents(colorSpace, colorList, locationList, 2);
// Draw Gradient Here
CGContextDrawLinearGradient(bitmapContext, myGradient, CGPointMake(0.0f, 0.0f), CGPointMake(320.0f, 480.0f), 0);
// Create a CGImage from context
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
// Create a UIImage from CGImage
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
// Release the CGImage
CGImageRelease(cgImage);
// Release the bitmap context
CGContextRelease(bitmapContext);
// Release the color space
CGColorSpaceRelease(colorSpace);
// Create the patterned UIColor and set as background color
[targetView setBackgroundColor:[UIColor colorWithPatternImage:image]];
虽然创建一个UIView 子类可能会更简单。它也会使用更少的内存。
【讨论】:
CGGradientCreateWithColors/CGGradientCreateWithColorComponents 创建的CGGradientRef
你可以这样做:
斯威夫特 >= 4.0:
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.red.cgColor, UIColor.white.cgColor]
view.layer.insertSublayer(gradient, at: 0)
Swift 3.0:
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.red().cgColor, UIColor.white().cgColor]
view.layer.insertSublayer(gradient, at: 0)
斯威夫特
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.redColor().CGColor, UIColor.whiteColor().CGColor]
view.layer.insertSublayer(gradient, atIndex: 0)
目标-C:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
确保也将 QuartzCore 框架添加到您的项目中(至少对于 Objective-C)...
#import <QuartzCore/QuartzCore.h>
【讨论】:
#import <QuartzCore/QuartzCore.h>
我同意 rpetrich,只做 UIView 子类会更干净。有关如何执行此操作的示例,请参阅my response in this question。如果你愿意,你可以创建一个通用的渐变 UIView 子类,然后简单地将它放在你想要渐变背景的视图后面。
【讨论】:
我将采用的答案更改为我的版本
+(void) setBlackGradientToView:(UIView*)targetView
{
CGRect frame = targetView.frame;
// Allocate color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//allocate myGradient
CGFloat locationList[] = {0.0f, 1.0f};
CGFloat colorList[] = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f};
CGGradientRef myGradient = CGGradientCreateWithColorComponents(colorSpace, colorList, locationList, 2);
// Allocate bitmap context
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, frame.size.width, frame.size.height, 8, 4 * frame.size.width, colorSpace, kCGImageAlphaPremultipliedLast);
//Draw Gradient Here
CGContextDrawLinearGradient(bitmapContext, myGradient, CGPointMake(0.0f, 0.0f), CGPointMake(0, frame.size.height), 0);
// Create a CGImage from context
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
// Create a UIImage from CGImage
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
// Release the CGImage
CGImageRelease(cgImage);
// Release the bitmap context
CGContextRelease(bitmapContext);
// Release the color space
CGColorSpaceRelease(colorSpace);
//Create the patterned UIColor and set as background color
[targetView setBackgroundColor:[UIColor colorWithPatternImage:uiImage]];
//return uiImage;
}
【讨论】: