iphone - 在上一个终点和下一个起点之间画一条线

标签 iphone objective-c ios xcode core-graphics

我被困在这个问题上了。我需要在前一个终点和下一个起点之间画一条线。我在两点之间画一条线的代码是

- (void)drawRect:(CGRect)rect
{
    CGContextRef c=UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 4.0);
    CGFloat red[4]={0.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 50.0f, 50.0f);
    CGContextAddLineToPoint(c, 100.0f, 100.0f);
    CGContextStrokePath(c) ;
}

我如何给出多个点,以便我可以将其显示为图表。 提前致谢

最佳答案

附带说明一下,Apple 更喜欢您使用高级 UIKit 方法而不是 Core Graphics 调用来绘制直线和曲线等简单的东西,因此您可以像这样重写您的方法:

[[UIColor redColor] setStroke];

UIBezierPath *linePath = [UIBezierPath bezierPath];

[linePath moveToPoint:CGPointMake(50.0, 50.0)];
[linePath addLineToPoint:CGPointMake(100.0, 100.0)];
[linePath setLineWidth:4.0];
[linePath stroke];

关于iphone - 在上一个终点和下一个起点之间画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578729/

相关文章:

ios - 使用 iPhone 6/iOS 8 读取 NFC 标签

ios - 在 iOS 中分析 Assets.car 文件

ios - 原型(prototype)可重用 tableview 单元格渲染 subview 很差

ios - 在 iOS 中,如何仅将一侧的标题图像拉伸(stretch)到 100%?

c# - ARC + 循环引用收集器与前沿 GC(比如 .NET 4.5 GC)

ios - 如何摆脱 "CocoaPods could not find compatible versions for pod"?我尝试更新 cocoapod 和 pod repo

iphone - 捆绑 ID 后缀?它是什么?

ios - 加载以前从相机胶卷中保存的图像

javascript - 确定 stringByEvaluatingJavaScriptFromString 何时完成

iOS 共享扩展 - 如何访问核心数据