我正在使用Quartz 2D绘图制作应用程序。我创建了两个文件:Draw2D.m和Draw2D.h。然后我在View Controller中创建了按钮。我为这个按钮创建了一个代码。但我的视图只显示黑屏。这是我的代码:
ViewController.m
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Draw2D.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)Start {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // от 0.0 до 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // от 0.5 до 1.0, от белого
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // от 0.5 до 1.0, от черного
self.view.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
Draw2D *draw2DView = [[Draw2D alloc] initWithFrame:(self.view.bounds)];
[self.view addSubview:draw2DView];
}
Draw2D.m
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context,
[UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
}
请注意,Draw2D没有与ViewController建立连接。而且我的drawRect什么都不做,它画了一个正方形。我的代码应该在随机背景上绘制蓝色方块。