作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!