Skip to content

Instantly share code, notes, and snippets.

@chamons
Created October 25, 2017 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamons/ab9a3e0f05ca800516e11b70f1450baf to your computer and use it in GitHub Desktop.
Save chamons/ab9a3e0f05ca800516e11b70f1450baf to your computer and use it in GitHub Desktop.
@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