我有一个 UIView,其中添加了 2 个 CALayers,[self.layer addSublayer:subLayerA]; //...
给出了以下视图层次结构:
UIView subclass
- backing layer (provided by UIView)
- subLayerA
- subLayerB
如果我在呈现 UIViewtouchesBegan
的视图控制器中覆盖它正确识别 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);
}
但是,如果我touchesBegan
在UIView中覆盖,其支持层是两个子层的父层,它将返回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);
}
有什么想法我哪里出错了吗?