开发者

Using UITouch inside a UIScrollView

Just toying with the SDK and I was wondering if possible a UITouch event can work inside a UIScrollView.

I have setup a UIScrollView which handles a large UIView, inside the UIView is a UIImageView, I've managed to get the UITouch to drag the UIImageView outside of the UIScrollView but inside it's not registering the event.

I suppose what I was trying to accomplish was dragging the UIImageView around the large UIView whilst the UIScrollView moves along the image if the user drags it beyond the POS of when the UIView when the UIImageView began it's dragging, if that makes sense?

Many开发者_高级运维 thanks


(If I understood the question correctly)

UIScrollView intercepts touchMoved events and does not propagate them to its contents if scrolling is enabled. So to do the dragging in the UIScrollView contents in my app I did the following trick:

touchesBegan: Check if you touch the "draggable" region. If YES - disable scrolling in UIScrollView.

touchesMoved: Now as scrolling is disabled your contents view receives this event and you can move your draggable UIImageView accordingly.

touchesEnded: Reenable scrolling in UIScrolliew.

If you want to drag the view outside of the visible UIScrollView area you may also need to check manually if you're near the boundaries and manually adjust contents offset (I didn't try this myself but I think it should work).


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.


(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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜