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 получавам грешката: No visible @interface for '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