如何在ScrollView上处理触摸事件,如UITableView

时间:2012-05-27 12:55:43

标签: iphone objective-c ios cocoa-touch ipad

我制作了一个客户控件,继承自UIView并在UIButton上添加了很多UIView个。 当用户触摸并移动时,我将做一些动画:让按钮按功能touchesMoved移动:

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

但是buttonClick事件似乎具有更高的优先级。

我希望它可以像UITableView,滚动的东西具有更高的优先级然后按钮点击。

1 个答案:

答案 0 :(得分:1)

您需要查看UIPanGestureRecognizer

它允许您取消发送给其他处理程序的事件。


更新了有关如何保护以前的观点的其他信息。

在动作回调中,您会收到初始触摸位置recognizer.state == UIGestureRecognizerStateBegan的通知。您可以将此点保存为实例变量。您还会以不同的时间间隔recognizer.state == UIGestureRecognizerStateChanged获得回调。您也可以保存此信息。然后,当您使用recognizer.state == UIGestureRecognizerStateEnded获得回调时,将重置所有实例变量。

- (void)handler:(UIPanGestureRecognizer *)recognizer
{
    CGPoint location = [recognizer locationInView:self];
    switch (recognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            self.initialLocation = location;
            self.lastLocation = location;
            break;
        case UIGestureRecognizerStateChanged:
            // Whatever work you need to do.
            // location is the current point.
            // self.lastLocation is the location from the previous call.
            // self.initialLocation is the location when the touch began.

            // NOTE: The last thing to do is set last location for the next time we're called.
            self.lastLocation = location;
            break;
    }
}

希望有所帮助。