当 CALayer 被触摸时触发动作?

发布于 2024-10-01 22:49:25 字数 1092 浏览 1 评论 0原文

所以我一直在寻找,但还没有找到我想要的东西。

我有一个视图,然后是该视图的子视图。在第二个视图上,我根据我给它的坐标创建 CALayers。我希望能够触摸这些 CALayers 中的任何一个并触发某些内容。

我发现了不同的代码片段,看起来它们可以提供帮助,但我无法实现它们。

例如:

-(void)touchesEnded:(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;

} } } 

还有这个......

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    // If the touch was in the placardView, bounce it back to the center
    if ([touch view] == placardView) {
        // Disable user interaction so subsequent touches don't interfere with animation
        self.userInteractionEnabled = NO;
        [self animatePlacardViewToCenter];
        return;
    }       
}

我仍然是这个东西的初学者。我想知道是否有人可以告诉我该怎么做。感谢您的任何帮助。

So I have been looking all over and I haven't quite found what I am looking for.

I have a view and then a subview of that view. On that second view I create CALayers depending on what coordinates I give it. I want to be able to touch any of those CALayers and trigger something.

I have found different pieces of code that look like they can help, but I haven't been able to implement them.

For example:

-(void)touchesEnded:(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;

} } } 

and also this....

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    // If the touch was in the placardView, bounce it back to the center
    if ([touch view] == placardView) {
        // Disable user interaction so subsequent touches don't interfere with animation
        self.userInteractionEnabled = NO;
        [self animatePlacardViewToCenter];
        return;
    }       
}

I am still pretty much a beginner to this stuff. I was wondering if anyone can tell me how to do this. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-10-08 22:49:25

CALayer 无法直接对触摸事件做出反应,但程序中的许多其他对象可以 - 例如托管图层的 UIView。

事件(例如触摸屏幕时系统生成的事件)通过所谓的“响应程序链”发送。因此,当触摸屏幕时,会向位于触摸位置的 UIView 发送一条消息(换句话说 - 调用方法)。对于触摸,存在三种可能的消息:touchesBegan:withEvent:touchesMoved:withEvent:touchesEnded:withEvent:

如果该视图没有实现该方法,系统将尝试将其发送到父视图(iOS 语言中的超级视图)。它正在尝试发送它,直到它到达顶视图。如果没有一个视图实现该方法,它将尝试传递到当前视图控制器,然后是它的父控制器,然后传递到应用程序对象。

这意味着您可以通过在任何这些对象中实现上述方法来对触摸事件做出反应。通常,托管视图或当前视图控制器是最佳候选者。

假设您在视图中实现它。下一个任务是确定您的哪个图层已被触摸,为此您可以使用方便的方法 convertPoint:toLayer:

例如,它在视图控制器中可能看起来像这样:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint p = [(UITouch*)[touches anyObject] locationInView:self.worldView];
    for (CALayer *layer in self.worldView.layer.sublayers) {
        if ([layer containsPoint:[self.worldView.layer convertPoint:p toLayer:layer]]) {
            // do something
        }
    }
}

CALayer cannot react on touch events directly, but lots of other objects in your program can - for example the UIView which is hosting the layers.

Events, such as an event that is generated by system when screen is touched, are being sent over so called "responder chain". So when the screen is touched, a message is sent (in other words - method is called) to the UIView which lies at the location of touch. For touches there are three possible messages: touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent:.

If that view does not implement the method, the system will try to send it to the parent view (superview in iOS language). It's trying to send it until it reaches the top view. If none of the views implement the method, it tries to be delivered to current view-controller, then it's parent controller, then to the application object.

That means you can react on touch events by implementing mentioned methods in any of these objects. Usually the hosting view or the current view-controller are the best candidates.

Let's say you implement it in the view. Next task is to figure out which of your layers have been touched, for this you can use convenient method convertPoint:toLayer:.

For example here's it might look like in a view controller:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint p = [(UITouch*)[touches anyObject] locationInView:self.worldView];
    for (CALayer *layer in self.worldView.layer.sublayers) {
        if ([layer containsPoint:[self.worldView.layer convertPoint:p toLayer:layer]]) {
            // do something
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文