xcode: нарисовать линию. как передать переменные

в моем 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, чтобы я мог рисовать разные линии?

Спасибо.


person suyama    schedule 13.04.2012    source источник


Ответы (2)


Создайте в своем классе DrawLine свойства, представляющие объекты, которыми вы хотите управлять. При создании нового объекта задайте его свойства, назначив их напрямую или передав их в пользовательском методе initWith.... Используйте значения свойств в drawRect:.

person Phillip Mills    schedule 13.04.2012
comment
Спасибо за ответ. В моем пользовательском (id)initWithFrame:(CGRect)frame lineColor:(UIColor *)lColor я передаю объекты. Но в ViewController я получаю сообщение об ошибке: «Нет видимого @interface для DrawLine» объявляет селектор «initWithFrame:lineColor:». Спасибо. - person suyama; 13.04.2012
comment
Поэтому вам нужно объявить метод в DrawLine.h. - person Phillip Mills; 13.04.2012

Вот у меня сработал код, я передал параметр lineWidth:

Файл 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

Файл DrawLine.m

...
- (id)initWithFrame:(NSRect)frameRect andLineWidth :(double)lineWidth0 andColor: (UIColor *) color0;
{
    self.lineWidth = lineWidth0;
    self = [super initWithFrame:frameRect];
if (self) {
    // Initialization code
    [super setBackgroundColor:color0];
}
return self;
    return self;
}
...

Файл 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];
}
...

Оно работает.

person Darius Miliauskas    schedule 16.04.2015