-[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
-[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
- Subject: -[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
- From: Steven Degutis <email@hidden>
- Date: Mon, 4 May 2009 17:19:52 -0500
Hello.
The basic problem boils down to -hitTest: returning a completely
incorrect layer when the layer-backed NSView with the root layer is
inside a scroll view.
I've provided some sample code to demonstrate this problem. To see it
in action, just put the code into an NSView class, and stick an NSView
in your NIB with the custom class name, and embed this in an
NSScrollView.
Specifically, you'll notice that the -hitTest: returns the correct
layer *only* if you click the view while it is fully visible in the
window, and before you have made it not-fully-visible for the first
time. After you resize the scroll view to hide any portion of this
NSView subclass, -hitTest: will consistently return the wrong layer
every time.
- (void) awakeFromNib {
[self setFrameSize:NSMakeSize(300, 300)];
[[self enclosingScrollView] setWantsLayer:YES];
int i;
for (i = 0; i < 6; i++) {
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, i * 50, 300, 50);
float r = (float)(random() % 100) / 100.0;
float g = (float)(random() % 100) / 100.0;
float b = (float)(random() % 100) / 100.0;
CGColorRef color = CGColorCreateGenericRGB(r,g,b,1.0);
layer.backgroundColor = color;
CGColorRelease(color);
layer.name = [NSString stringWithFormat:@"%d", i];
[self.layer addSublayer:layer];
}
}
- (void) drawRect:(NSRect)rect {
[[[NSColor greenColor] colorWithAlphaComponent:0.5]
drawSwatchInRect:[self bounds]];
}
- (void) mouseDown:(NSEvent*)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
CALayer *layer = [self.layer hitTest:NSPointToCGPoint(point)];
NSLog(@"%@", [layer name]);
}
I've tried to replace `self` with the enclosing scroll view, or its
content view or document view, in the -mouseDown: method, and many
combinations of this. Nothing seems to do the trick. I've also not
seen anybody have this same problem on cocoa-dev, google, et al.
Thanks in advance for any help!
-Steven Degutis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden