使用Quartz时如何拆除UIView的锯齿?

时间:2013-05-28 04:22:11

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

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

// 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];

但总是它周围有一些锯齿。它可能只有一个像素。但它看起来非常可怕。像这样: The head button with black saw tooth around it

是否有人提示去除黑锯齿?

1 个答案:

答案 0 :(得分:0)

参加派对的方式较晚,但从过去的经验来看,这是一个角落半径和边界的错误。我尝试用它来创建围绕视图的圆形边框,并注意到类似的出血。

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

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];

如果要避免多个视图,可以在图像视图的边框层添加其他背景图层。这需要让它受到负面影响,以便它吸引图像。