1

我想画一条线

我的代码是这个

-(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];

}

任何人都可以帮助我吗?

4

3 回答 3

3

你应该继承 aUIView并将你的画线代码移动到drawRect:方法中,就像你正在做的那样(只是希望它用于 UIView 子类而不是 UIViewController)

- (void)drawRect:(CGRect)rect {

   [super drawRect:rect];

   // Place your code here

}

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



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

于 2010-08-28T07:07:07.973 回答
0

您不能直接调用 drawRect 。

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

于 2010-08-28T18:54:22.823 回答
0

如果有需要打电话

- (void)drawRect:(CGRect)rect

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

[self.view setNeedsDisplay];

然后

- (void)drawRect:(CGRect)rect

方法会自动被调用

于 2013-01-21T11:56:19.847 回答