iphone - 使用 Quartz 时如何去掉 UIView 的锯齿?

标签 iphone ios cocoa-touch uiview quartz-graphics

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

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

有人有一些技巧可以去除黑色锯齿吗?

最佳答案

派对迟到了,但从过去的经验来看,这是一个角落半径和边界的错误。我尝试使用它在 View 周围创建圆形边框,并注意到类似的出血。

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

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

如果您想避免多个 View ,您可以在 ImageView 的边框层中添加一个额外的背景层。那将需要将其边界反向插入,以便在图像周围绘制。

关于iphone - 使用 Quartz 时如何去掉 UIView 的锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783814/

相关文章:

iphone - 如何使用 valueforkey 作为 if 语句参数

ios - 在 Storyboard 中创建自定义 UITableview 节标题的最佳方法

iphone - 带有自定义字体的 UILabel 显示 *错误* 自定义字体

cocoa - iOS 背景音频图标

ios - NSOperation和NSOperationQueue iOS的后台任务

ios - 横向模式下的 UIImagePickerViewController

iphone - 为什么必须导入我的委托(delegate)头文件?

iphone - 如何确定应用程序首次安装或使用的日期?

ios - 苹果自己的 ASLR 实现是如何工作的?

iphone - 使用名称截断 NSString