关于iPhone:如何在CALayer上获取触摸事件?

How to get touch event on a CALayer?

我是iPhone SDK的新手。现在,我正在使用CALayers进行编程,我真的非常喜欢它,它不比UIViews昂贵,并且比OpenGL ESsprite要少得多的代码。

我有一个问题:可以在CALayer上获得触摸事件吗?我了解如何使用

在UIView上获取触摸事件

1
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

但是我找不到关于如何在CALayer对象上获取触摸事件的任何信息,例如,漂浮在3D空间中的橙色正方形。我拒绝相信我是唯一对此感到好奇的人。

感谢您的帮助!


好吧-回答了我自己的问题!假设您在视图控制器的主层中有一堆CALayers,并且希望它们在触摸时变为不透明度0.5。在您的视图控制器类的.m文件中实现此代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 1) {
        for (UITouch *touch in touches) {
            CGPoint point = [touch locationInView:[touch view]];
            point = [[touch view] convertPoint:point toView:nil];

            CALayer *layer = [(CALayer *)self.view.layer.presentationLayer hitTest:point];

            layer = layer.modelLayer;
            layer.opacity = 0.5;
        }
    }
}


类似于第一个答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (CALayer *)layerForTouch:(UITouch *)touch {
    UIView *view = self.view;

    CGPoint location = [touch locationInView:view];
    location = [view convertPoint:location toView:nil];

    CALayer *hitPresentationLayer = [view.layer.presentationLayer hitTest:location];
    if (hitPresentationLayer) {
        return hitPresentationLayer.modelLayer;
    }

    return nil;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CALayer *hitLayer = [self layerForTouch:touch];

    // do layer processing...
}

Egor T答案已为Swift 4更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    super.touchesBegan(touches, with: event)

    if let touch = touches.first, let touchedLayer = self.layerFor(touch)
    {
        //Here you will have the layer as"touchedLayer"
    }
}

private func layerFor(_ touch: UITouch) -> CALayer?
{
    let view = self.view
    let touchLocation = touch.location(in: view)
    let locationInView = view.convert(touchLocation, to: nil)

    let hitPresentationLayer = view.layer.presentation()?.hitTest(locationInView)
    return hitPresentationLayer?.model()
}

我发现我的

坐标不正确

1
point = [[touch view] convertPoint:point toView:nil];

我不得不将其更改为

1
point = [[touch view] convertPoint:point toView:self.view];

获取正确的图层