【问题标题】:Make Background of UIView a Gradient Without Sub Classing使 UIView 的背景成为没有子类的渐变
【发布时间】:2010-10-25 02:07:09
【问题描述】:

有没有办法让 UIView 的背景变成渐变而不继承它?我也不想使用图像文件来完成此操作。仅仅为了为背景绘制渐变就必须继承 UIView 似乎很迟钝。

【问题讨论】:

    标签: iphone uiview subclass


    【解决方案1】:

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

    【讨论】:

    • 第 2 行存在内存泄漏,没有机会释放 CGColorSpaceCreateDeviceRGB() 的结果
    • 很好的实现。不过有一个问题,这是否适用于新的视网膜显示设备? (我看到那里是硬编码的 320x480)
    • @chakrit,它仍然适用于视网膜设备,但只能以 1x 分辨率渲染渐变;取决于可能足够的梯度。 320x480 是硬编码的,以使示例更简单 - 最好使用实际视图大小。
    • @AlexanderPerechnev 这是您使用CGGradientCreateWithColors/CGGradientCreateWithColorComponents 创建的CGGradientRef
    【解决方案2】:

    你可以这样做:

    斯威夫特 >= 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>
    

    【讨论】:

    • 谢谢。光滑如丝
    • 当我使用此代码时,我遇到了类似架构 i386 的未定义符号的错误:“_OBJC_CLASS_$_CAGradientLayer”,引用自:
    • @VinothKumar 添加#import &lt;QuartzCore/QuartzCore.h&gt;
    • 我写了这个“cellForRowAtIndexPath”,这样我就可以在每个单元格中显示渐变视图,它工作正常,现在我遇到了一个问题,当我上下滚动时,渐变颜色变暗了还有更暗的。如何解决这个问题?
    • @jithin 我会用您正在使用的代码发布一个问题。我认为您每次都在添加图层,并且它被 tableView 重用,因此每次都使其颜色变深
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我将采用的答案更改为我的版本

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

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 2020-08-27
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        • 1970-01-01
        相关资源
        最近更新 更多