首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置渐变UITableViewCell背景?

如何设置渐变UITableViewCell背景?
EN

Stack Overflow用户
提问于 2010-07-19 01:26:15
回答 2查看 7.7K关注 0票数 7

我想创建一个像iPhone上的默认时钟应用程序一样的渐变UITableViewCell背景。我不太确定如何做到这一点。我是否要创建一个镜像并设置它:

代码语言:javascript
运行
复制
cell.contentView.backgroundColor = [UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]];

还是有其他/更好的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-19 04:16:26

请参阅this tutorial/code by Matt Coneybeare

票数 7
EN

Stack Overflow用户

发布于 2011-10-18 14:35:54

我找到了这个问题的现有答案,但并不满意。这里有一种替代方法,它需要更少的代码,不需要覆盖任何方法,并且可以应用于任何视图。这个想法是在UIView的层中添加一个CAGradientLayer作为子层,我在任何地方都找不到这种方法,所以我想分享一下。

CAGradientLayer添加到任意UIView,如下所示:

UIView报头上的类别- UIView+Gradient.h:

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>

@interface UIView (Gradient)
-(void) addLinearUniformGradient:(NSArray *)stopColors;
@end

UIView implementation UIView+Gradient.m上的类别:

代码语言:javascript
运行
复制
#import "UIView+Gradient.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (Gradient)

-(void) addLinearUniformGradient:(NSArray *)stopColors
{
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    gradient.colors = stopColors;
    gradient.startPoint = CGPointMake(0.5f, 0.0f);
    gradient.endPoint = CGPointMake(0.5f, 1.0f);    
    [self.layer addSublayer:gradient];
}

@end

如何在创建UITableViewCell后在UITableViewCell的backgroundView上设置渐变

代码语言:javascript
运行
复制
// Set the gradient for the cell's background
CGRect backgroundViewFrame = cell.contentView.frame;
backgroundViewFrame.size.height = yourCellHeight;
cell.backgroundView = [[UIView alloc] initWithFrame:backgroundViewFrame];
[cell.backgroundView addLinearUniformGradient:[NSArray arrayWithObjects:
                                               (id)[[UIColor redColor] CGColor],
                                               (id)[[UIColor greenColor] CGColor], nil]];

此示例仅显示了如何设置简单的两色点渐变(具有均匀间距)。快速浏览一下CAGradientLayer文档将向您展示如何设置更复杂的渐变。

(有关其他重要信息*,请编辑*)

另一件要记住的事情是,如果你在tableView:cellForRowAtIndexPath这样的方法中添加渐变层,UITableViewCells通常会被重用(即tableView dequeueReusableCellWithIdentifier:@"foo")。因为单元格正在被重复使用,所以如果每次单元格出列时都要向表视图单元格添加渐变层,那么在单元的整个生命周期中,您可能会向该表视图单元格添加多个渐变层。这可能是性能降低的原因之一。此问题可能会在可见结果没有任何更改的情况下发生,因此很难检测/观察。有几种方法可以解决这个问题,但您可以考虑做的一件事是修改上面的原始代码,除了添加CAGradientLayer之外,还将创建的CAGradientLayer返回给调用者。然后,编写另一个类别方法相当简单,如果渐变层实际上包含在视图中,则允许将其移除:

代码语言:javascript
运行
复制
-(BOOL) removeGradient:(CAGradientLayer *)gradientLayer
{
    // Search for gradient layer and remove it
    NSArray *layers = [self.layer sublayers];
    for ( id layer in layers ) {
        if ( layer == gradientLayer ) {
            [gradientLayer removeFromSuperlayer];
            return YES;
        }
    }

    // Failed to find the gradient layer in this view
    return NO;
}

并不是所有用例都需要移除梯度,但是如果您的用例导致单元重用,您可能需要考虑移除梯度。可以考虑在UITableViewCell的prepareForReuse方法中调用此方法。

我很抱歉,我最初的解决方案没有解决这个问题。但由于它可能是相当微妙的东西,我想用这个额外的信息更新我的答案。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3276504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档