我在自定义 UIButton 中使用 drawRect 来绘制一个带有图像的边框按钮。代码如下:
- (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw a rectangle
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetStrokeColorWithColor(context, [[UIColor grayColor] CGColor]);
//Define a rectangle
CGRect drawrect = CGRectMake(CGRectGetMinX(rect),CGRectGetMinY(rect),rect.size.width,rect.size.height);
CGContextStrokeRect(context,drawrect);}
问题是在角落我得到了一个额外的像素(见附图)。我做错了什么?
谢谢
最佳答案
您是沿着像素的边缘绘制矩形,而不是沿着像素的中心绘制。所以你的矩形只覆盖了大多数像素的一半。在角落,它覆盖了四分之三的像素。
要沿着像素中心绘制,您必须使用半整数坐标。试试这个:
CGContextStrokeRect(context, CGRectInset(rect, 0.5, 0.5));
关于IOS:drawRect 在角上显示 1px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056330/