微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在iOS中绘制单点

我想知道绘制单点线的最好方法是什么?
我的目标是在tableViewCell中绘制这个行,使其看起来像本机单元格分隔符.
我不想使用本机分隔符,因为我想使用不同的颜色和不同的位置(而不是底部).

起初我正在使用一个1px的UIView,并将其着色为灰色.但在Retina显示它看起来像2px.
也尝试使用这种方法

- (void)drawLine:(CGPoint)startPoint endPoint:(CGPoint)endPoint inColor:(UIColor *)color {

    CGMutablePathRef straightLinePath = CGPathCreateMutable();
    CGPathMovetoPoint(straightLinePath,NULL,startPoint.x,startPoint.y);
    CGPathAddLinetoPoint(straightLinePath,endPoint.x,endPoint.y);

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = straightLinePath;
    UIColor *fillColor = color;
    shapeLayer.fillColor = fillColor.CGColor;
    UIColor *strokeColor = color;
    shapeLayer.strokeColor = strokeColor.CGColor;
    shapeLayer.linewidth = 0.5f;
    shapeLayer.fillRule = kCAFillRuleNonZero;
    [self.layer addSublayer:shapeLayer];
}

它有60%的时间因为某种原因而工作吗?有什么问题吗?
无论如何,我很乐意听到一个更好的方法.

谢谢.

解决方法

我做了一个UIView类别.这是我的方法
#define SEParaTOR_HEIGHT 0.5

- (void)addSeparatorLinesWithColor:(UIColor *)color
{
    [self addSeparatorLinesWithColor:color edgeInset:UIEdgeInsetsZero];
}


- (void)addSeparatorLinesWithColor:(UIColor *)color edgeInset:(UIEdgeInsets)edgeInset
{

    UIView *topSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(edgeInset.left,- SEParaTOR_HEIGHT,self.frame.size.width - edgeInset.left - edgeInset.right,SEParaTOR_HEIGHT)];
    [topSeparatorView setBackgroundColor:color];
    [self addSubview:topSeparatorView];

    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(edgeInset.left,self.frame.size.height + SEParaTOR_HEIGHT,SEParaTOR_HEIGHT)];
    [separatorView setBackgroundColor:color];
    [self addSubview:separatorView];
}

为了增加雷米的好回答,这可能更简单.做一个班UILine.m

@interface UILine:UIView
@end
@implementation UILine
-(id)awakeFromNib
    {
    // careful,contentScaleFactor does NOT WORK in storyboard during initWithCoder.
    // example,float sortaPixel = 1.0/self.contentScaleFactor ... does not work.
    // instead,use mainScreen scale which works perfectly:
    float sortaPixel = 1.0/[UIScreen mainScreen].scale;
    UIView *topSeparatorView = [[UIView alloc] initWithFrame:
        CGRectMake(0,self.frame.size.width,sortaPixel)];

    topSeparatorView.userInteractionEnabled = NO;
    [topSeparatorView setBackgroundColor:self.backgroundColor];
    [self addSubview:topSeparatorView];

    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = NO;
    }
@end

在IB中,放入UIView,单击身份检查器,并将该类重命名为UILine.在IB中设置所需的宽度.将高度设置为1或2像素 – 只需要在IB中查看.在IB中设置您想要的背景颜色.当您运行应用程序时,它将成为该像素的1像素线,该宽度. (你可能不会受到故事板/ xib中的任何自动调整设置的影响,我无法让它破裂.)你完成了

注意:您可能会认为“为什么不在awakeFromNib的代码中调整UIView的大小?”加载后,在故事板应用程序中调整视图的大小是有问题的 – 请参阅这里的许多问题!

有趣的gotchya:你可能只是把UIView,比如说,在故事板上的10或20像素高,只是你可以看到它.当然,它在应用程序中消失,你会得到漂亮的一个像素线.但!一定要记住self.userInteractionEnabled = NO,否则可能会超出你的其他按钮!

2016解决方案! https://stackoverflow.com/a/34766567/294884

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐