gpt4 book ai didi

iphone - 检测 UIScrollView 中图层的触摸

转载 作者:行者123 更新时间:2023-12-03 20:00:36 24 4
gpt4 key购买 nike

我有一个 UIScrollView,其中包含任意数量的缩略图,这些缩略图应检测触摸事件、对自身执行 3D 转换并调用委托(delegate)方法。

我无法克服的一个问题是检测哪个子层被窃听。我将 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 方法。并出现错误“请求成员‘名称’不是结构或联合”。

最佳答案

调用[scrollView.layer hitTest:touchPoint];。返回值将是被触摸的层。如果返回的图层等于 scrollView.layer,则没有子图层被点击。

关于iphone - 检测 UIScrollView 中图层的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3824269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com