iPhone:cornerRadius 质量下降
我有一个 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.
(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每次调用 cellForRowAtIndexPath 时,您都会创建一个新的
count
UILabel。最终,在同一位置将出现多个具有相同抗锯齿曲线的重叠视图,因此它看起来会是块状的。尝试仅在创建新单元格时在
if (cell == nil)
块中创建count
UILabel。否则,按标签获取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 theif (cell == nil)
block. Otherwise, get thecount
label by tag.检查表格视图/单元格是否设置为在绘制之前清除其上下文。我注意到单元格上的文本也有类似的问题。
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.
我看到了一些奇怪的问题,其中看起来基于核心动画的属性是累积的,尽管它们不应该是累积的。这里的问题可能是由于每次返回单元格时重复更改角半径值而导致的某种累积蠕变引起的。
我建议在再次设置之前测试圆角半径是否已经等于 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.