iPhone:cornerRadius 质量下降

发布于 2024-09-05 04:36:34 字数 1681 浏览 6 评论 0原文

我有一个 UILabel,我使用cornerRadius 在图层上创建一个半径。最终目标是让标签看起来像苹果在邮件应用程序中所做的那样。

一开始看起来很棒,但是一旦你深入到该行并返回几次,圆边的质量就开始下降。您可以在屏幕截图中看到,左侧是块状的。

为什么会出现这种情况呢?这似乎是在加载该视图大约 2 次后发生的。

替代文本
(来源:puc.edu

这是我的单元格创建方法:

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

    static NSString *CellIdentifier = @"Cell";

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

    Trip *trip = [fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = trip.title;
    cell.detailTextLabel.text = @"Date of trip";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Create a nice number, like mail uses
    UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 50, 12, 34, 20)];
    [count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];
    [count setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    count.textAlignment = UITextAlignmentCenter;
    count.backgroundColor = [UIColor grayColor];
    count.textColor = [UIColor whiteColor];
    count.layer.cornerRadius = 10;
    [cell addSubview:count];
    [count release];

    return cell;

}

I have a UILabel that I create a radius on the layer, using cornerRadius. The ultimate goal is to make the label look like Apple does in the mail app.

It looks great at first, but once you drill down into that row and back a few times, the quality of the rounded edge starts to degrade. You can see in the screen shot, the left side is blocky.

Why would this be happening? It seems to happen after about 2 times of loading that view.

alt text
(source: puc.edu)

Here is my cell creation method:

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

    static NSString *CellIdentifier = @"Cell";

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

    Trip *trip = [fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = trip.title;
    cell.detailTextLabel.text = @"Date of trip";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Create a nice number, like mail uses
    UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 50, 12, 34, 20)];
    [count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];
    [count setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    count.textAlignment = UITextAlignmentCenter;
    count.backgroundColor = [UIColor grayColor];
    count.textColor = [UIColor whiteColor];
    count.layer.cornerRadius = 10;
    [cell addSubview:count];
    [count release];

    return cell;

}

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

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

发布评论

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

评论(3

只是在用心讲痛 2024-09-12 04:36:34

在每次调用 cellForRowAtIndexPath 时,您都会创建一个新的 count UILabel。最终,在同一位置将出现多个具有相同抗锯齿曲线的重叠视图,因此它看起来会是块状的。

尝试仅在创建新单元格时在 if (cell == nil) 块中创建 count UILabel。否则,按标签获取 count 标签。

if ( cell == nil ) {
  cell = ...
  count = ...
  ...
  count.tag = 'coun';
  [cell.contentView addSubview:count];
  [count release];
} else {
  count = (UILabel *)[cell viewWithTag:'coun'];
}

[count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];

In every call to cellForRowAtIndexPath, you are creating a new count UILabel. Eventually there will be several overlapping views in the same place with the same antialiased curve, so it will look blocky.

Try creating the count UILabel only when a new cell is created, in the if (cell == nil) block. Otherwise, get the count label by tag.

if ( cell == nil ) {
  cell = ...
  count = ...
  ...
  count.tag = 'coun';
  [cell.contentView addSubview:count];
  [count release];
} else {
  count = (UILabel *)[cell viewWithTag:'coun'];
}

[count  setText:[NSString stringWithFormat:@"%i",[[trip.rides allObjects] count]]];
追星践月 2024-09-12 04:36:34

检查表格视图/单元格是否设置为在绘制之前清除其上下文。我注意到单元格上的文本也有类似的问题。

Check to see if the tableview/cell is set to clear its context before drawing. I noticed I had similar issues with text on the cell.

虫児飞 2024-09-12 04:36:34

我看到了一些奇怪的问题,其中看起来基于核心动画的属性是累积的,尽管它们不应该是累积的。这里的问题可能是由于每次返回单元格时重复更改角半径值而导致的某种累积蠕变引起的。

我建议在再次设置之前测试圆角半径是否已经等于 10。 (尽管我希望通过向上和向下滚动而不是重新加载视图来显示更多内容。)

另一个可能的问题是某些子视图正在迁移,从而导致视觉伪影。

编辑:

而不是将标签添加到单元格本身。尝试将其添加为单元格内容视图的子视图。

I've seen some strange issues wherein it looks like Core Animation based properties are accumulative even though they shouldn't be. Your problem here might be caused by some kind of accumulative creep from changing the value of the corner radius repeatedly every time the cell is returned.

I would suggest testing if the corner radius already equals 10 before setting it again. (Although I would expect that to show up more with scrolling up and down than in reloading the view.)

Another possible problem would be that some subview is migrating causing a visual artifact.

Edit:

Instead of adding the label to the cell itself. Try adding it as a subview of the cell's content view.

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