在视图中绘制线:不绘制

发布于 2024-09-16 05:27:31 字数 602 浏览 5 评论 0原文

我正在尝试画一条线,

我的代码是这样的

-(void) drawRect:(CGRect) rect
{
    NSLog(@"Reached Draw ARect Function");

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 2.0, 0, 1); 
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint( ctx, 100,100);

    CGContextStrokePath(ctx);

}

,我从 viewDidLoad 调用如下,

- (void)viewDidLoad {

    [super viewDidLoad];
    CGRect k = CGRectMake(1.0, 1.0, 100.0, 200.0);
    [self.view drawRect:k];

}

有人可以帮助我吗?

I am trying to draw a line

my Code is this

-(void) drawRect:(CGRect) rect
{
    NSLog(@"Reached Draw ARect Function");

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 2.0, 0, 1); 
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint( ctx, 100,100);

    CGContextStrokePath(ctx);

}

and i am calling from the viewDidLoad as follows

- (void)viewDidLoad {

    [super viewDidLoad];
    CGRect k = CGRectMake(1.0, 1.0, 100.0, 200.0);
    [self.view drawRect:k];

}

Can any one help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞎闹 2024-09-23 05:27:31

您应该子类化 UIView 并将线条绘制代码移动到 drawRect: 方法,就像您正在做的那样(只是希望它是针对 UIView 子类而不是 UIViewController)

- (void)drawRect:(CGRect)rect {

   [super drawRect:rect];

   // Place your code here

}

您必须还要确保您在 InterfaceBuilder 中使用 UIView 子类作为您将绘制到的视图。您不必自己调用此方法。如果您想要为某些内容设置动画,您应该调用 [view setNeedsDisplay]; 来请求重绘视图。

...并且...您应该在调用 CGContextMoveToPoint 之前添加 CGContextBeginPath(ctx);

You should subclass a UIView and move your line drawing code to the drawRect: method, like you are doing (just hoping it's for a UIView subclass and not the UIViewController)

- (void)drawRect:(CGRect)rect {

   [super drawRect:rect];

   // Place your code here

}

You must also make sure you use the UIView subclass in InterfaceBuilder for the view you will be drawing into. You should not have to call this method yourself. If you want to animate something you should call the [view setNeedsDisplay]; to request a redraw of the view.

...and...you should add CGContextBeginPath(ctx); just before you call CGContextMoveToPoint

陌伤ぢ 2024-09-23 05:27:31

您不能直接调用drawRect。

相反,您可以在 UIView 上调用 setNeedsDisplay,返回并等待操作系统稍后使用适当的上下文调用 UIView 的 drawRect。

You can't call drawRect directly.

Instead you call setNeedsDisplay on your UIView, return, and wait for the OS to call the UIView's drawRect with a proper context at some later time.

远昼 2024-09-23 05:27:31

如果需要

- (void)drawRect:(CGRect)rect

在用户拖动特定视图时动态调用方法,只需放置以下代码:

[self.view setNeedsDisplay];

然后

- (void)drawRect:(CGRect)rect

方法将自动被调用

If there is a need to call

- (void)drawRect:(CGRect)rect

method dynamically while user is dragging a particular view, just place the following code:

[self.view setNeedsDisplay];

then

- (void)drawRect:(CGRect)rect

method will automatically be called

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文