1

只是玩弄 SDK,我想知道 UITouch 事件是否可以在 UIScrollView 中工作。

我已经设置了一个处理大型 UIView 的 UIScrollView,在 UIView 内部是一个 UIImageView,我设法让 UITouch 将 UIImageView 拖到 UIScrollView 之外,但在它内部没有注册事件。

我想我想要完成的是在大 UIView 周围拖动 UIImageView 而 UIScrollView 沿着图像移动,如果用户将它拖动到 UIImageView 开始拖动时的 POS 之外,如果这有意义吗?

非常感谢

4

3 回答 3

4

(如果我正确理解了这个问题)

如果启用滚动, UIScrollView 会拦截touchMoved事件并且不会将它们传播到其内容。因此,为了在我的应用程序中拖动 UIScrollView 内容,我做了以下技巧:

touchesBegan: 检查您是否触摸了“可拖动”区域。如果是 - 禁用 UIScrollView 中的滚动。

touchesMoved: 现在,当滚动被禁用时,您的内容视图会收到此事件,您可以相应地移动可拖动的 UIImageView。

touchesEnded: 在 UIScrolliew 中重新启用滚动。

如果您想将视图拖到可见的 UIScrollView 区域之外,您可能还需要手动检查您是否靠近边界并手动调整内容偏移量(我自己没有尝试过,但我认为它应该可以工作)。

于 2010-03-14T21:32:06.383 回答
0

The best way to implement this I found was to subclass the uiscrollview class itself and then add the touches began event method in the your subclass. This worked for me.

于 2012-04-05T16:21:22.960 回答
0
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

NSLog(@"DEBUG: Touches began" );

UITouch *touch = [[event allTouches] anyObject];

[super touchesBegan:touches withEvent:event];
}

(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

NSLog(@"DEBUG: Touches cancelled");

// Will be called if something happens - like the phone rings

UITouch *touch = [[event allTouches] anyObject];

[super touchesCancelled:touches withEvent:event];

}


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

NSLog(@"DEBUG: Touches moved" );

UITouch *touch = [[event allTouches] anyObject];

[super touchesMoved:touches withEvent:event];

}

(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];

//Number of touches on the screen
switch ([allTouches count])
{
    case 1:
    {
        //Get the first touch.
        UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

        switch([touch tapCount])
        {
            case 1://Single tap

                break;
            case 2://Double tap.

                break;
        }
    }
        break;
}
[super touchesEnded:touches withEvent:event];
}
于 2012-05-15T10:29:31.947 回答