UIControlEventTouch...?

发布于 2024-08-07 04:49:20 字数 155 浏览 7 评论 0原文

我有一个图像/按钮网格,我希望用户能够将手指拖动到图像/按钮上,以便当他们的手指触摸每个图像/按钮时调用一个事件。

我该怎么办?

我现在能想到的最好的例子是“联系人”应用程序,您如何将手指向下拖动字母列表(右侧),当您触摸每个字母时,它会跳转到联系人列表的该部分。

I have a grid of images/buttons, and I want the user to be able to drag their finger over the images/buttons such that when their finger touches each image/button an event is called.

How do I do this???

The best example I can think of now is the Contacts app, how you drag your finger down the list of letters (on the right) and as you touch each letter it jumps to that part of the contacts list.

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

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

发布评论

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

评论(1

风和你 2024-08-14 04:49:20

您需要在 UIViewController 中实现 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 。每当触摸在屏幕上移动时都会调用此方法。然后,您可以使用坐标(使用 [myUITouch locationInView:self.view])或 [self.view.layer hitTest:[myUITouch locationInView:self.view]]获取发生触摸的图像/按钮并从中进行工作。

例如,如果我有一行 10 个图像,每个图像 32 像素 x 32 像素,并且我想记录每个图像被触摸的时间,我可以执行如下操作:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGSize buttonSize = myButton.bounds.size;
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    if (touchPoint.y < buttonSize.y) {
        NSInteger buttonNumber = touchPoint.x/buttonSize.x;
        NSLog(@"Button number %d pushed", buttonNumber);
    }
}

请注意,这假设多点触摸被禁用,否则它将随机选择一键录音

You're going to want to implement -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event in your UIViewController. This method is called any time a touch moves on the screen. You can then either use the coordinates (use [myUITouch locationInView:self.view]) or [self.view.layer hitTest:[myUITouch locationInView:self.view]] to get the image/button the touch occurred in and work from that.

For example, if I have a row of ten images, each 32 pixels by 32 pixels, and I want to record when each of them is touched, I could do something like the following:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGSize buttonSize = myButton.bounds.size;
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    if (touchPoint.y < buttonSize.y) {
        NSInteger buttonNumber = touchPoint.x/buttonSize.x;
        NSLog(@"Button number %d pushed", buttonNumber);
    }
}

Note this assumes multitouch is disabled or it will randomly pick one touch to record

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文