Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@interface ViewController : NSViewController
@end
@interface MyView : NSView
@property NSNumber * DrawnLineWidth;
@end
#import "ViewController.h"
@import QuartzCore;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView * v = [[MyView alloc] initWithFrame: CGRectMake (0, 0, 250, 250)];
[self.view addSubview:v];
}
@end
@implementation MyView
{
NSColor * color;
NSBezierPath * path;
NSNumber * drawnLineWidth;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (instancetype)initWithFrame:(NSRect)frameRect
{
if (!(self = [super initWithFrame:frameRect]))
return nil;
color = NSColor.blueColor;
path = [[NSBezierPath alloc] init];
[path moveToPoint:self.bounds.origin];
[path lineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
self.DrawnLineWidth = @1;
self.wantsLayer = true;
self.layer.backgroundColor = [[NSColor grayColor] CGColor];
return self;
}
- (void)mouseDown:(NSEvent *)theEvent {
[self animator].DrawnLineWidth = [[NSNumber alloc] initWithInt:(int)([self.DrawnLineWidth integerValue] + 10)];
}
- (void)setDrawnLineWidth:(NSNumber *) value {
[self willChangeValueForKey:@"DrawnLineWidth"];
drawnLineWidth = value;
path.lineWidth = [value floatValue];
[self setNeedsDisplay:YES];
[self didChangeValueForKey:@"DrawnLineWidth"];
}
- (NSNumber *)DrawnLineWidth {
return drawnLineWidth;
}
- (void)drawRect:(NSRect)dirtyRect
{
[color setStroke];
[path stroke];
}
+ (id)defaultAnimationForKey:(NSString *)key {
if ([key isEqualToString:@"drawnLineWidth"]) {
return [CABasicAnimation animation];
} else {
return [super defaultAnimationForKey:key];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment