如何在iOS中绘制单点

前端之家收集整理的这篇文章主要介绍了如何在iOS中绘制单点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@
我想知道绘制单点线的最好方法是什么?
我的目标是在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

原文链接:https://www.f2er.com/iOS/336215.html

猜你在找的iOS相关文章