【问题标题】:How to remove the saw tooth of UIView when using Quartz?使用 Quartz 时如何去掉 UIView 的锯齿?
【发布时间】:2013-05-22 22:30:59
【问题描述】:

我想在视图中添加一个头部按钮,周围有一些空白区域。我的代码是这样的:

// head button
UIButton * btnHead = [UIButton buttonWithType:UIButtonTypeCustom];
btnHead.frame = CGRectMake(0, 0, self.frame.size.width - property.userhead_cell_space / 2, self.frame.size.width  - property.userhead_cell_space / 2);
btnHead.clipsToBounds = YES;
btnHead.layer.cornerRadius = btnHead.bounds.size.width / 2;
btnHead.layer.borderColor = [UIColor whiteColor].CGColor;
btnHead.layer.borderWidth = (isPad?4.0f:2.0f);
btnHead.layer.contentsScale = [[UIScreen mainScreen] scale];
btnHead.layer.backgroundColor = [UIColor whiteColor].CGColor;

[btnHead addTarget:self action:@selector(clickHead:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:btnHead];

但它周围总是有锯齿。它可能只有一个像素。但是看起来很可怕。像这样:

谁有去除黑锯齿的小窍门?

【问题讨论】:

  • 而不是用石英芯做这个,你为什么不把图像转换成一个有白色边框的圆圈..
  • @lukya 因为他们总是改变按钮的大小、边框宽度和其他属性。我应该找到一些常用的方法来完成这个。
  • 您总是可以使用 Core Graphics 并覆盖按钮的 drawRect 来做到这一点。我做过类似的事情(使用 CG)并且没有出现“锯齿”。
  • 为什么要设置内容比例?对于视图层,您不需要这样做。你的坐标都是偶数吗,所以你的半径或框架没有使用一半大小?

标签: iphone ios cocoa-touch uiview quartz-graphics


【解决方案1】:

聚会晚了,但根据过去的经验,这是一个角落半径和边界的错误。我尝试使用它在视图周围创建圆形边框,并注意到类似的出血。

一种解决方法是为边框创建一个视图,然后为图像创建一个稍微嵌入的视图。

UIView* border = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
border.backgroundColor = UIColor.whiteColor; // border color
border.layer.cornerRadius = border.bounds.size.width / 2;
border.layer.masksToBounds = YES;

// inset by the desired border width
UIImageView* image = [[UIImageView alloc] initWithFrame:CGRectInset(border.bounds, 2, 2)];
image.image = someImage;
image.layer.cornerRadius = image.bounds.size.width / 2;
image.layer.masksToBounds = YES;

[border addSubview:image];
[self addSubview:border];

如果您想避免多个视图,您可以在图像视图的边框层中添加一个额外的背景层。这需要它的边界负插入,以便它在图像周围绘制。

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多