objective c - Why is my hitTest in UIView not identifying CALayer touched -
i have uiview in 2 calayers added [self.layer addsublayer:sublayera]; //...
giving following view hierarchy:
uiview subclass - backing layer (provided uiview) - sublayera - sublayerb
if override touchesbegan
in view controller presents uiview correctly identifies calayer touched:
// 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 copy of touchedlayer calayer *actuallayer = [touchedlayer modellayer]; // returns actual calayer touched nslog (@"touchpoint: %@", nsstringfromcgpoint(touchpoint)); nslog (@"touchedlayer: %@", touchedlayer); nslog (@"actuallayer: %@", actuallayer); }
however, if override touchesbegan
in uiview backing layer parent of 2 sublayers, return null
calayer (though gives correct touchpoint):
// 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 copy of touchedlayer calayer *actuallayer = [touchedlayer modellayer]; // returns actual calayer touched nslog (@"touchpoint: %@", nsstringfromcgpoint(touchpoint)); nslog (@"touchedlayer: %@", touchedlayer); nslog (@"actuallayer: %@", actuallayer); }
any ideas going wrong?
i had same problem..
calayer's hittest method requires location in coordinates of receiver's super layer.
so adding following line should fix it: touchpoint = [self.layer convertpoint: touchpoint tolayer: self.layer.superlayer]
this explain why test [sublayera hittest:touchpoint] works (touchpoint in coordinate space "self", parent of sublayera)
hope helps.
Comments
Post a Comment