如何检测点击了哪个 CALayer?
How to detect which CALayer was clicked on?
我在检测哪个 CALayer 被点击时遇到问题,因为这些 CALayer 有一个常量 CABasicAnimation 移动 x 和 y 位置。
我目前的代码如下:
-(void)mouseUp:(NSEvent *)theEvent
{
CGPoint pointInView = NSPointToCGPoint([self convertPoint:[theEvent locationInWindow]fromView:nil]);
CALayer* clickedOn = [(CALayer*) self.layer hitTest:[self.layer convertPoint:pointInView toLayer:self.layer.superlayer]];
int selectedContact = -1;
for (int i = 0; i < [contactLayers count]; i++) {
CALayer* presentationLayer = [contactLayers[i] presentationLayer];
if (presentationLayer == clickedOn) {
selectedContact = i;
break;
}
}
if(selectedContact == -1)
return; //no contact selected;
CALayer* selectedContactLayer = contactLayers[selectedContact];
[selectedContactLayer removeFromSuperlayer];
}
contactLayers 是一个 NSMutableArray,包含用户可以点击的所有可能的 CALayer。
每次运行时,"i" 似乎总是以 -1 结束。我正在使用 presentationLayer
因为 CALayers 应用了 CABasicAnimation。我也试过modelLayer
,但这只有在你点击每一层的初始位置时才有效。
所以只是回顾一下:我有一个 CALayers 的 NSMutableArray,它们都应用了 CABasicAnimation,这个数组称为 contactLayers。当用户点击一个图层时,我需要通过将索引设置为数组中的适当值来知道他们点击的图层。提前致谢!
通过命中测试'self.layer.presentationLayer'计算'clickedOn',而不是'self.layer'
我在检测哪个 CALayer 被点击时遇到问题,因为这些 CALayer 有一个常量 CABasicAnimation 移动 x 和 y 位置。
我目前的代码如下:
-(void)mouseUp:(NSEvent *)theEvent
{
CGPoint pointInView = NSPointToCGPoint([self convertPoint:[theEvent locationInWindow]fromView:nil]);
CALayer* clickedOn = [(CALayer*) self.layer hitTest:[self.layer convertPoint:pointInView toLayer:self.layer.superlayer]];
int selectedContact = -1;
for (int i = 0; i < [contactLayers count]; i++) {
CALayer* presentationLayer = [contactLayers[i] presentationLayer];
if (presentationLayer == clickedOn) {
selectedContact = i;
break;
}
}
if(selectedContact == -1)
return; //no contact selected;
CALayer* selectedContactLayer = contactLayers[selectedContact];
[selectedContactLayer removeFromSuperlayer];
}
contactLayers 是一个 NSMutableArray,包含用户可以点击的所有可能的 CALayer。
每次运行时,"i" 似乎总是以 -1 结束。我正在使用 presentationLayer
因为 CALayers 应用了 CABasicAnimation。我也试过modelLayer
,但这只有在你点击每一层的初始位置时才有效。
所以只是回顾一下:我有一个 CALayers 的 NSMutableArray,它们都应用了 CABasicAnimation,这个数组称为 contactLayers。当用户点击一个图层时,我需要通过将索引设置为数组中的适当值来知道他们点击的图层。提前致谢!
通过命中测试'self.layer.presentationLayer'计算'clickedOn',而不是'self.layer'