如何检测点击哪个CALayer?

时间:2016-07-12 05:27:18

标签: objective-c cocoa calayer cabasicanimation

我在检测点击哪个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,包含用户可以点击的所有可能的CALayers。

每次运行时,“我”似乎总是保持-1。我正在使用presentationLayer,因为CALayers已经应用了CABasicAnimation。我也试过了modelLayer,但这只有在点击每个图层的初始位置时才有效。

所以只需回顾一下:我有一个CAMutableArray的CALayers都应用了CABasicAnimation,这个数组叫做contactLayers。当用户点击图层时,我需要通过将索引设置为数组中的适当值来知道他们点击了哪个图层。先谢谢!

1 个答案:

答案 0 :(得分:1)

通过点击测试'self.layer.presentationLayer'计算'clickedOn',而不是'self.layer'