UITableViewCell 中的自定义 UIView 中的 CAGradientLayer 问题

发布于 2024-09-24 02:25:01 字数 1835 浏览 1 评论 0原文

我的情况有些特殊,需要帮助。我有一个 CustomView,我将其放置在 UITableView 中的每个 UITableViewCell 中。那里没有问题。

在我的 CustomView 中有一个 UILabel,它显示给它的一些文本。根据文本的长度,CustomView 的宽度会增大/缩小。一切都很好。

我遇到的问题是我的 CustomView 中的一个子层,它是一个渐变(CAGradientLayer)。对于不在视图中的单元格,渐变大小不会在屏幕外增大/缩小。

让我向您展示一些代码...我像这样创建我的 CustomView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    CustomView *custom = [[CustomView alloc] init];
    custom.tag = 777;
    [cell.contentView addSubview:custom];
    [custom release];

}

}

在这个阶段我不 initWithFrame 因为单元格可以被重用并且宽度总是错误的。一旦单元格在那里,我调用一个方法configureCell:,我在其中执行以下操作:

CGSize teamMaxSize = CGSizeMake(300, 18);
CGSize teamNewSize = [[managedObject valueForKey:@"category"] 
    sizeWithFont:[UIFont systemFontOfSize:kFontSizeTeam]
    constrainedToSize:teamMaxSize
    lineBreakMode:UILineBreakModeWordWrap];

CustomView *lozenge = (CustomView *) [cell.contentView viewWithTag:777];
lozenge.frame = CGRectMake(10, 5, teamNewSize.width+12, 18);
[lozenge resetSize];
[lozenge setLabel:[managedObject valueForKey:@"category"]];

这工作正常,我的CustomView的宽度是根据“类别”键的长度设置的。重置大小;上面的方法只是将我之前提到的 UILabel 的大小调整为 CustomView 的新框架,然后再填充值。

无论如何,回到我的问题。我正在 CustomView 的 drawRect: 方法中创建背景渐变。它工作正常,但是初始化时 CustomView 的大小。调整大小后,我需要调整gradient.frame的大小。因此,我尝试

gradient.frame = self.bounds

在上述代码中添加 When I call resetSize 。然而,当滚动 UITableView 时,我可以看到渐变加载到视图中时从左到右填充。无论如何,我是否可以让这种情况发生在“屏幕外”,以便 CustomView 一出现就填充渐变?

I have a somewhat special situation that I need help with. I have a CustomView which I am placing in every UITableViewCell in my UITableView. No problems there.

Within my CustomView is a UILabel which displays some text given to it. Depending on the length of the text, the width of the CustomView grows/shrinks. All fine.

The problem I am having is with a sublayer in my CustomView which is a gradient (CAGradientLayer). The gradient size will not grow/shrink off screen on cells not in view.

Let me show you some code... I create my CustomView like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    CustomView *custom = [[CustomView alloc] init];
    custom.tag = 777;
    [cell.contentView addSubview:custom];
    [custom release];

}

}

I don't initWithFrame at this stage as the cell could get reused and the width is always wrong. Once the cell is there, I call a method configureCell: where I do the following:

CGSize teamMaxSize = CGSizeMake(300, 18);
CGSize teamNewSize = [[managedObject valueForKey:@"category"] 
    sizeWithFont:[UIFont systemFontOfSize:kFontSizeTeam]
    constrainedToSize:teamMaxSize
    lineBreakMode:UILineBreakModeWordWrap];

CustomView *lozenge = (CustomView *) [cell.contentView viewWithTag:777];
lozenge.frame = CGRectMake(10, 5, teamNewSize.width+12, 18);
[lozenge resetSize];
[lozenge setLabel:[managedObject valueForKey:@"category"]];

This works fine, the width of my CustomView is setup as per the length of the 'category' key. The resetSize; method above simply resizes the UILabel I mentioned before to the new frame of the CustomView before I fill it with the value.

Anyway, back to my issue. I am creating the background gradient in the drawRect: method of my CustomView. It works fine, however is the size of the CustomView at init. After I resize I need to resize the gradient.frame. So, I tried adding

gradient.frame = self.bounds

When I call resetSize in my aforementioned code. However when scrolling through the UITableView I can see the gradient literally filling from left to right as it loads into view. Is there anyway I can get this to happen 'off screen' so the CustomView is filled with the gradient as soon as it appears?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-10-01 02:25:01

您是否考虑过使用CAGradientLayer作为自定义视图的图层类,而不是在drawRect:中创建渐变,例如:

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

然后您可以在您的初始化器,通过 self.layer。

Instead of creating a gradient in drawRect:, have you considered using CAGradientLayer as the layer class of your custom view, e.g.:

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

Then you can perform any setup of your gradient in your initializer, via self.layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文