使用设置为相同宽度的核心图形绘制的线在绘制时有时大小会有所不同 [英] Lines drawn with core graphics that are set to the same width sometimes vary in size when drawn

查看:87
本文介绍了使用设置为相同宽度的核心图形绘制的线在绘制时有时大小会有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在UITableViewCell中绘制的具有相同功能,相同宽度和颜色
的两条线的图片

Here is a picture of two lines drawn in a UITableViewCell with the same function, and same width, and color

您可以看到底线比另一行粗得多。

As you can see the bottom line is a lot thicker than the other line.

我用于绘制的代码:

    [CSDrawing drawLineWithColor:[UIColor blackColor] width:1.0 yPosition:1.0 rect:rect];
    [CSDrawing drawLineWithColor:[UIColor blackColor] width:1.0 yPosition:CGRectGetMaxY(rect) - 3.0 rect:rect]; // draw a line on top and bottom

    +(void)drawLineWithColor:(UIColor *)color width:(CGFloat)width yPosition:(CGFloat)yPosition rect:(CGRect)rect {

          CGContextRef context = UIGraphicsGetCurrentContext();
          CGContextSaveGState(context);

          CGContextMoveToPoint(context, 0.0, yPosition);
          CGContextAddLineToPoint(context, CGRectGetMaxX(rect), yPosition);

          CGContextSetStrokeColorWithColor(context, color.CGColor);
          CGContextSetLineWidth(context, width);

          CGContextStrokePath(context);

          CGContextRestoreGState(context);
     }


推荐答案

问题出在backgroundView在单元被重用时被拉伸以适合单元的内容。当单元更大时,像素被拉伸。解决方法是将 contentMode 属性设置为 UIViewContentModeRedraw

The problem was with the backgroundView being stretched to fit the content of the cell when the cell was being reused. When the cell was bigger the pixels were stretched. This is solved be setting the contentMode property to UIViewContentModeRedraw

这篇关于使用设置为相同宽度的核心图形绘制的线在绘制时有时大小会有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆