Я работаю над системой управления проектами, и я делаю древовидную диаграмму, где каждый узел является NSView, а соединения между ними также являются NSView. Есть ли какой-либо простой способ проверить, не щелкнули ли NSView с нарисованной линией? Это источник VSTonnection NSView. Я пытаюсь получить цвет нажатой точки, но пока ничего не добился. Другая идея состоит в том, чтобы использовать 4 линейных уравнений:Обнаружение щелчка по строке, нарисованной в UIView
#import "VTConnection.h"
#import "VTNodeAbstract.h"
@implementation VTConnection
-(void)drawMe {
}
-(struct VTConnectionStructORM) bluePrint {
VTConnectionStructORM conn;
conn.connection_id = self.connection_id;
conn.start_pos_x = self.start_pos.x;
conn.start_pos_y = self.start_pos.y;
conn.end_pos_x = self.end_pos.x;
conn.end_pos_y = self.end_pos.y;
return conn;
}
-(void) drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
/*
[self.layer setBorderWidth:2.0f];
[self.layer setBorderColor:[NSColor grayColor].CGColor];
[self.layer setCornerRadius:10.0];*/
[[NSColor clearColor] set];
[self.layer setOpaque:NO];
// NSRectFill(dirtyRect);
// return;
// get the initial context
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
// save the current state, as we'll overwrite this
// CGContextSaveGState(context);
[[NSColor grayColor] set];
// draw stuff
NSPoint outerStart = [self convertPoint:self.start_pos fromView:[self superview]];
NSPoint outerEnd = [self convertPoint:self.end_pos fromView:[self superview]];
CGContextMoveToPoint(context, outerStart.x, outerStart.y);
CGContextSetLineWidth(context, 3);
CGContextAddLineToPoint(context, outerEnd.x, outerEnd.y);
// do the actual drawing
CGContextStrokePath(context);
// [self setWantsLayer:YES];
// NSLog(@" drawing connection %@ --> %@", NSStringFromPoint(self.start_pos), NSStringFromPoint(self.end_pos));
}
-(id) initWithORM:(NSDictionary *) dict {
self = [super init];
if (self != nil) {
self.connection_id = [dict[@"connection"] intValue];
self.start_pos = NSMakePoint([dict[@"start_pos_x"] floatValue], [dict[@"start_pos_y"] floatValue]);
self.end_pos = NSMakePoint([dict[@"end_pos_x"] floatValue], [dict[@"end_pos_y"] floatValue]);
}
self.layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0, 0);
return self;
}
-(void)mouseDown:(NSEvent *)theEvent {
NSLog(@" click on connection %@", theEvent);
NSBitmapImageRep *test = [self bitmapImageRepForCachingDisplayInRect:self.frame];
NSPoint pointInMyFrame = [self convertPoint:theEvent.locationInWindow fromView:[self superview]];
NSLog(@" color %f %f" , pointInMyFrame.x ,pointInMyFrame.y);
NSLog(@" color %@",[test colorAtX:(int)pointInMyFrame.x y:(int)pointInMyFrame.y]);
}
-(void) redrawWithPoints:(NSPoint)A B:(NSPoint)B {
NSRect rect;
if (A.x > B.x) {
rect.origin.x = B.x;
self.start_pos = B;
self.end_pos = A;
} else {
rect.origin.x = A.x;
self.start_pos = A;
self.end_pos = B;
}
if (A.y > B.y) {
rect.origin.y = B.y;
} else {
rect.origin.y = A.y;
}
rect.size.width = abs(A.x-B.x);
rect.size.height = abs(A.y-B.y);
self.frame = rect;
// [self setNeedsDisplayInRect:rect];
}
-(BOOL)acceptsFirstResponder {
return YES;
}
- (BOOL)isOpaque {
// If the background color is opaque, return YES
// otherwise, return NO
return NO;
}
-(NSString *) description {
return [NSString stringWithFormat:@"%@ connection %i", [self class], self.connection_id];
}
@end
диаграмма изображения http://iccode.co.uk/vt/sof/diagram.png
Если вопрос говорит 'UIView', почему вы добавили тег' osx'? – LuisCien