在不使用子类的情况下使UIView成为渐变的背景

时间:2009-05-10 04:55:27

标签: iphone uiview subclass

有没有办法让UIView的背景成为渐变而不进行子类化?我宁愿不使用图像文件来实现这一点。只是为了为背景绘制一个渐变而必须将UIView子类化似乎很愚蠢。

4 个答案:

答案 0 :(得分:33)

您可以使用+[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子类可能更简单。它也将使用更少的内存。

答案 1 :(得分:29)

你可以这样做:

Swift> = 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)

Swift< = 2.3:

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>

答案 2 :(得分:1)

我同意rpetrich,只是做UIView子类会更干净。有关如何执行此操作的示例,请参阅my response in this question。如果您愿意,可以创建一个通用渐变UIView子类,并将其放在您想要具有渐变背景的视图后面。

答案 3 :(得分:0)

我将通过的答案更改为我的版本

+(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;
}