[self.layer addSublayer:subLayerA]; //...
次のビュー階層を与える2 つの CALayers が追加された UIView があります。
UIView subclass
- backing layer (provided by UIView)
- subLayerA
- subLayerB
UIView を表示するビュー コントローラーでオーバーライドtouchesBegan
すると、触れた 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);
}
ただし、バッキング レイヤーが 2 つのサブレイヤーの親であるUIViewでオーバーライドtouchesBegan
すると、CALayer が返されます (ただし、正しい touchPoint が得られます)。null
// 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);
}
私が間違っているアイデアはありますか?