Cheatography
https://cheatography.com
Assumes "Automatic Reference Counting" (ARC) is enabled.
Class Interfaces@interface Shape : NSObject | @property (assign) NSPoint center; | @property (strong) NSColor* color; | @end | | @interface Rectangle : Shape | @property (assign) NSSize size; | @end | | @interface Circle : Shape | @property (assign) CGFloat radius; | @end |
| | Class Implementation@implementation Shape | @synthesize centre; | @synthesize color; | - (void)draw { | [color set]; | } | @end | | @implementation Rectangle | @synthesize size; | - (void)draw { | [super draw]; | [NSBezierPath fillRect:NSMakeRect(self.centre.x - size.width / 2, self.centre.y - size.height / 2, size.width, size.height)]; | } | @end | | @implementation Circle | @synthesize radius; | - (void)draw { | [super draw]; | [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(self.centre.x - radius, self.centre.y - radius, radius2, radius2)] fill]; | } | @end |
| | Class Use- (void)drawRect:(NSRect)rect { | rect = [self bounds]; | [[NSColor cyanColor] set]; | [NSBezierPath fillRect:rect]; | | Rectangle* rectangle = [[Rectangle alloc] init]; | rectangle.size = NSMakeSize(300, 200); | rectangle.centre = NSMakePoint(NSMidX(rect), NSMidY(rect)); | rectangle.color = [NSColor whiteColor]; | [rectangle draw]; | | Circle* circle = [[Circle alloc] init]; | circle.radius = 60; | circle.centre = rectangle.centre; | circle.color = [NSColor redColor]; | [circle draw]; | } |
|
Created By
chocolatapp.com
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets