繁体   English   中英

在视图中绘制线:未绘制

[英]Draw Line in View : Not Drawing

我正在画一条线

我的密码是这个

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

}

有人可以帮我吗?

您应该子类化UIView ,然后像执行操作一样将线条绘制代码移动到drawRect:方法(只是希望它用于UIView子类而不是UIViewController)。

- (void)drawRect:(CGRect)rect {

   [super drawRect:rect];

   // Place your code here

}

您还必须确保对要绘制到的视图使用InterfaceBuilder中的UIView子类。 您不必自己调用此方法。 如果要制作动画,则应调用[view setNeedsDisplay]; 请求重绘视图。



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

如果需要打电话

- (void)drawRect:(CGRect)rect

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

[self.view setNeedsDisplay];

然后

- (void)drawRect:(CGRect)rect

方法将被自动调用

您不能直接调用drawRect。

取而代之的是,您在UIView上调用setNeedsDisplay,返回,然后等待操作系统稍后在适当的上下文中调用UIView的drawRect。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM