UIControlEvents触摸......?

时间:2009-10-14 08:23:50

标签: iphone uiimageview uibutton uitouch uicontrol

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

我该怎么做?

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

1 个答案:

答案 0 :(得分:0)

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

例如,如果我有一行十个图像,每个32像素乘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);
    }
}

请注意,这假设多点触控已禁用,或者会随机选择一键进行录制