【问题标题】:draw a line by using CGPath [closed]使用CGPath画一条线[关闭]
【发布时间】:2011-03-25 15:53:16
【问题描述】:

如何使用 CGPath 画线?

【问题讨论】:

  • 很难说出这里问的是什么?除了提问者想知道如何绘制表示一条线的 CGPath 之外,还有什么其他解释?
  • 画线的方法有很多种。 Core Graphics、Quartz、OpenGL、SpriteKit、GLKit、SceneKit、GL 着色器——仅举几例。我们必须猜测 OP 的意图/首选框架。
  • 假设 Core Graphics 不安全吗,因为他引用的是 CGPath?

标签: iphone core-graphics


【解决方案1】:

由于您并没有真正指定如何画一条带路径的线,所以我只是给您举个例子。

在左上角和右下角(在 iOS 上)之间用 UIView 的 drawRect 中的路径绘制一条对角线:

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGPathCloseSubpath(path);
    CGContextAddPath(ctx, path);
    CGContextSetStrokeColorWithColor(ctx,[UIColor whiteColor].CGColor);
    CGContextStrokePath(ctx);
    CGPathRelease(path);
}

【讨论】:

    【解决方案2】:

    theView.h

    #import <UIKit/UIKit.h>
    
    @interface theView : UIView {
    }
    
    @end
    

    theView.m

    #import "theView.h"
    
    @implementation theView
    
    -(void)drawRect:(CGRect)rect {
    
        CGContextRef context = UIGraphicsGetCurrentContext(); 
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetLineWidth(context, 2.0);
        CGContextMoveToPoint(context,0,0);
        CGContextAddLineToPoint(context,20,20);
        CGContextStrokePath(context);
    }
    
    @end
    

    创建上述文件。
    基于窗口的应用:添加新的 UIView 并将其类更改为 theView。
    基于视图的应用:将 UIView 类更改为 theView。
    最后点击“构建并运行”:)

    结果:红色对角线。

    【讨论】:

    • 不应该也放出路径什么的吗?
    • 我认为你没有回答这个问题。 OP 想知道如何创建一个表示一条线的 CGPath 并绘制该 CGPath。
    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多