0

I'm making app with Quartz 2D drawing. I created a two files: Draw2D.m and Draw2D.h. Then I created button in my View Controller. I created a code to this button. But my view displays only black screen. Here is my code:

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);
}

Please notice that Draw2D don't have connection with ViewController. And my drawRect don't do nothing, it draw a square. My code should draw blue square on the random background.

AMHD
  • 15
  • 1
  • 1
  • 4

0 Answers0