检测UIScrollView中图层的触摸

时间:2010-09-29 17:29:13

标签: iphone uiscrollview calayer

我有一个UIScrollView,它包含任意数量的缩略图,可以检测触摸事件,对自己执行3D转换并调用委托方法。

我无法克服的一个问题是检测正在点击哪个子层。我将layer.name设置为索引计数,UIScrollView的子类确实获得了触摸事件。现在最后一个障碍是如何在子图层上执行hitTest以获取名称属性和瞧!

我在考虑将UIView子类化为CALayer的容器并以这种方式捕获触摸事件。但我希望可能有更经济的方法来确定被触摸的子层。

我一直在尝试的代码(在我的UIScrollView子类中):

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 1) {
        for (UITouch *touch in touches) {

            CGPoint point = [touch locationInView:[touch view]];
            CALayer *alayer = [self.layer hitTest:point];
            if (alayer) NSLog(@"layer %@ touched",alayer.name);
            else NSLog(@"layer not detected");

        } 
    } 
}

我收到两个编译错误。首先警告:没有找到hitTest方法。并且错误“请求成员'名称'不是结构或联合”。

1 个答案:

答案 0 :(得分:2)

致电[scrollView.layer hitTest:touchPoint];。返回值将是触摸的图层。如果返回的图层等于scrollView.layer,则不会点击任何子图层。

相关问题