ios - 在 UIView 中画线

标签 ios iphone uiview

我需要在 UIView 中绘制一条水平线。最简单的方法是什么。例如,我想在 y-coord=200 处画一条黑色水平线。

我没有使用 Interface Builder。

最佳答案

也许这有点晚了,但我想补充一点,还有更好的方法。 使用 UIView 很简单,但是比较慢。此方法会覆盖 View 自身的绘制方式并且速度更快:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0f);

    CGContextMoveToPoint(context, 0.0f, 0.0f); //start at this point

    CGContextAddLineToPoint(context, 20.0f, 20.0f); //draw to this point

    // and now draw the Path!
    CGContextStrokePath(context);
}

关于ios - 在 UIView 中画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3128752/

相关文章:

ios - UITableViewCell背景色动画

ios - UITabBarController tabBar setItemWidth 不起作用

ios - 为什么通行证后面的App无法加载

ios - iOS 增强现实中的多个文本

iphone - 创建条件转场

ios - 如何在旋转中使用 TapGestureRecognizer 处理 UIImage

ios - 在 Swift 的过渡变化后移动东西

ios - iOS8 中的扩展 - 由于没有系统应用程序而终止

iOS 7 如何自定义按钮 UIActionSheet

ios - 如何在 touchesBegan 上调整自定义 UIView 中的 UILabel 位置?