Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xcode:画线。如何传递变量_Xcode_Variables_Line_Draw - Fatal编程技术网

xcode:画线。如何传递变量

xcode:画线。如何传递变量,xcode,variables,line,draw,Xcode,Variables,Line,Draw,在我的ViewController中,我有一个按钮: - (IBAction)drawLineClick:(id)sender { CGRect rect; rect.origin.x = 20.0f; rect.origin.y = 40.0f; rect.size.width = 100.0f; rect.size.height = 100.0f; //draw line DrawLine *drawLine = [[DrawLin

在我的ViewController中,我有一个按钮:

- (IBAction)drawLineClick:(id)sender 
{
    CGRect rect;
    rect.origin.x = 20.0f;
    rect.origin.y = 40.0f;
    rect.size.width = 100.0f;
    rect.size.height = 100.0f;

    //draw line
    DrawLine *drawLine = [[DrawLine alloc] initWithFrame:rect]; 
    [self.view addSubview:drawLine];
}
在我的DrawLine课程中,我只画了一条线:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [super setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code

    [self drawLine];
}

- (void)drawLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 3.0);
    CGContextMoveToPoint(context, 0, 0); 
    CGContextAddLineToPoint(context, 50, 50); 
    CGContextStrokePath(context);
}
这很有效,但这不是可变的。每次都是同一条线。 如何将线条颜色、线条宽度等从ViewController传递到DrawLine类,以便绘制不同的线条


谢谢。

在DrawLine类中创建表示要控制的内容的属性。创建新对象时,可以通过直接指定属性或在自定义
initWith…
方法中传递属性来设置其属性。使用drawRect:中的属性值。

这是为我编写的代码,我传递了线宽参数:

DrawLine.h文件

#import <Cocoa/Cocoa.h>

@interface DrawLine : NSView
@property (nonatomic, strong) double *lineWidth;
@property (nonatomic, strong) UIColor *color;
- (void)drawRect:(CGRect)rect;
- (id)initWithFrame:(NSRect)frameRect andLineWidth :(double)lineWidth0 andColor: (UIColor *) color0;
...
@end
ViewController.m文件

...
- (IBAction)drawLineClick:(id)sender 
{
    CGRect rect;
    rect.origin.x = 20.0f;
    rect.origin.y = 40.0f;
    rect.size.width = 100.0f;
    rect.size.height = 100.0f;

    double lineWidth = 10;
    UIColor *color = [UIColor clearColor];

    //draw line
    DrawLine *drawLine = [[DrawLine alloc] initWithFrame:rect andLineWidth: lineWidth andColor: color]; 
    [self.view addSubview:drawLine];
}
...

这很有效。

谢谢你的回答。在我的自定义“(id)initWithFrame:(CGRect)frame lineColor:(UIColor*)lColor”中,我传递对象。但是在ViewController中,我得到了一个错误:“DrawLine”没有可见的@interface声明选择器“initWithFrame:lineColor:”。谢谢。所以您需要在DrawLine.h中声明方法。
...
- (IBAction)drawLineClick:(id)sender 
{
    CGRect rect;
    rect.origin.x = 20.0f;
    rect.origin.y = 40.0f;
    rect.size.width = 100.0f;
    rect.size.height = 100.0f;

    double lineWidth = 10;
    UIColor *color = [UIColor clearColor];

    //draw line
    DrawLine *drawLine = [[DrawLine alloc] initWithFrame:rect andLineWidth: lineWidth andColor: color]; 
    [self.view addSubview:drawLine];
}
...