Quartz 2d试图重绘三角形

时间:2011-05-30 17:15:50

标签: iphone quartz-graphics quartz-2d

我正在开发一款iPhone应用程序。

我有以下课程:

#import "Triangulo.h"


@implementation Triangulo

@synthesize minusValue;


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

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    CGSize size = rect.size;
    CGPoint origin = {10.0f, size.height - 10.0f};
    [self drawLineAtOrigin:origin withLength:size.width - 10.0f];

    CGPoint origin2 = {10.0f, size.height - 20.0f};
    CGSize size2 = {size.width - minusValue, 20.0f};
    [self drawTriangleAtOrigin:origin2 withSize: size2 inRect: rect];
}

- (void)drawLineAtOrigin:(CGPoint)origin withLength:(float)length {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    CGContextMoveToPoint(context, origin.x, origin.y);
    CGContextAddLineToPoint(context, length, origin.y);
    CGContextStrokePath(context);
}

- (void)drawTriangleAtOrigin:(CGPoint)origin withSize:(CGSize)size inRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextClearRect(UIGraphicsGetCurrentContext(), rect);

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

    CGContextMoveToPoint(context, origin.x, origin.y);
    CGContextAddLineToPoint(context, size.width, origin.y);
    CGContextAddLineToPoint(context, size.width, origin.y - size.height);
    CGContextAddLineToPoint(context, origin.x, origin.y);
    CGContextFillPath(context);
    CGContextStrokePath(context);

}

- (void)dealloc {
    [super dealloc];
}


@end

这个UIView是UIViewController.view的一部分。我将UIViewController.h添加为:

IBOutlet Triangulo *triangulo;

...

@property (nonatomic, retain) IBOutlet Triangulo *triangulo;

在UIViewController上我有一个修改Triangulo的按钮:

- (IBAction)btnDrawTriangleClicked:(id)sender {

    triangulo.minusValue += 10.0f;

    CGRect rect = CGRectMake(0, 0, 280, 50);

    [triangulo drawRect: rect];
}

但我无法画出任何东西,因为UIGraphicsGetCurrentContext总是为零。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您不应该直接致电drawRect:。每当要重绘视图时,都应该调用setNeedsDisplaysetNeedsDisplayInRect:。传递给rect的{​​{1}}不是传递的随机值。它告诉您需要重绘的视图边界部分。

答案 1 :(得分:1)

将此代码添加到Triangulo班级:

- (void)setMinusValue:(double)aMinusValue {
    minusValue = aMinusValue;
    [self setNeedsDisplay];
}

然后将btnDrawTriangleClicked:方法改为:

- (IBAction)btnDrawTriangleClicked:(id)sender {
    triangulo.minusValue += 10.0f;
}

现在当你改变minusValue时,Triangulo会自动将自己标记为需要重新显示。 (有关详细信息,请参阅The View Drawing Cycle)。正如迪帕克所说,你永远不会自己致电drawRect:(除非你在drawRect:方法本身,并且你正在呼叫[super drawRect:frame];)。 drawRect:方法由系统自动调用,并始终确保预先设置有效的CGGraphicsContext。由于您是直接调用它,因此该准备工作尚未完成,因此UIGraphicsGetCurrentContext()通常会返回NULL