我在这2个CALayers添加[self.layer addSublayer:subLayerA]; //...
给予以下视图层次一个UIView:为什么我在UIView的则hitTest未识别的CALayer感动
UIView subclass
- backing layer (provided by UIView)
- subLayerA
- subLayerB
如果我在视图控制器覆盖touchesBegan
那正确地提出了UIView它标识CALayer的感动:
// in view controller
#import <QuartzCore/QuartzCore.h>
//.....
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CALayer *touchedLayer = [self.view.layer.presentationLayer hitTest:touchPoint]; // returns a copy of touchedLayer
CALayer *actualLayer = [touchedLayer modelLayer]; // returns the actual CALayer touched
NSLog (@"touchPoint: %@", NSStringFromCGPoint(touchPoint));
NSLog (@"touchedLayer: %@", touchedLayer);
NSLog (@"actualLayer: %@", actualLayer);
}
然而,如果我在的UIView,其底层是两个子层的父覆盖touchesBegan
,它将返回null
的CALayer的(虽然提供了正确的接触点):
// in UIView subclass
#import <QuartzCore/QuartzCore.h>
//.....
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
CALayer *touchedLayer = [self.layer.presentationLayer hitTest:touchPoint]; // returns a copy of touchedLayer
CALayer *actualLayer = [touchedLayer modelLayer]; // returns the actual CALayer touched
NSLog (@"touchPoint: %@", NSStringFromCGPoint(touchPoint));
NSLog (@"touchedLayer: %@", touchedLayer);
NSLog (@"actualLayer: %@", actualLayer);
}
任何想法我哪里错了?
这是一个没有道理,总是有道理的。 A +。如果可以的话,我会给你买一瓶啤酒。 – 2015-07-14 18:34:23