如何在CALayer上获得触摸事件? [英] How to get touch event on a CALayer?

查看:174
本文介绍了如何在CALayer上获得触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone SDK的新手。现在我正在用CALayers进行编程,我真的非常喜欢 - 不如UIViews那么贵,而且比OpenGL ES精灵少了许多代码。

I'm new to the iPhone SDK. Right now I'm programming with CALayers which I really like a lot – not as expensive as UIViews, and a lot less code than OpenGL ES sprites.

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

I have this question: is it possible to get a touch event on a CALayer? I understand how to get a touch event on a UIView with

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

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

but I can't find anywhere about how to get a touch event on a CALayer object, for instance, an orange square floating in 3D space. I refuse to believe I'm the only one who's curious about this.

我感谢任何帮助!

推荐答案

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

ok- answered my own question! let's say you've got a bunch of CALayers in your view controller's main layer, and you want them to go to opacity 0.5 when you touch them. implement this code in the .m file of your view controller class:

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

这篇关于如何在CALayer上获得触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆