• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
-[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

-[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

  • Follow-Ups:
    • Re: -[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
      • From: Jim Correia <email@hidden>
  • Prev by Date: Re: Integer as key in NSMutableDictionary
  • Next by Date: Re: drawing image
  • Previous by thread: Developing with Java
  • Next by thread: Re: -[CALayer hitTest:] incorrect when embedding layer-backed NSView subclass inside NSScrollView
  • Index(es):
    • Date
    • Thread