gpt4 book ai didi

iphone - 如何在 CALayer 上获取触摸事件?

转载 作者:行者123 更新时间:2023-12-03 18:18:22 27 4
gpt4 key购买 nike

我是 iPhone SDK 新手。现在我正在使用我非常喜欢的 CALayers 进行编程 - 不像 UIViews 那么昂贵,并且比 OpenGL ES sprites 的代码少得多。

我有这个问题:是否可以在 CALayer 上获取触摸事件?我了解如何在 UIView 上获取触摸事件

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

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

感谢任何帮助!

最佳答案

好的-回答了我自己的问题!假设您的 View Controller 的主层中有一堆 CALayer,并且您希望在触摸它们时将它们的不透明度设置为 0.5。在 View Controller 类的 .m 文件中实现此代码:

-(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;
}
}
}

关于iphone - 如何在 CALayer 上获取触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/573372/

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