0

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

我该怎么做呢???

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

4

1 回答 1

0

您将要-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event在您的 UIViewController 中实现。每当触摸在屏幕上移动时,都会调用此方法。然后,您可以使用坐标(使用[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);
    }
}

请注意,这假设多点触控已禁用,否则它将随机选择一次触控进行记录

于 2010-01-21T23:57:14.133 回答