Crisn
Crisn

Reputation: 243

Objective C: Drag and Drop on UIScrollView

I am trying to make a drag and drop function on UIScrollView and it's quite confusing for me in my stage because i'm a beginner. i tried using touch event but i failed (though it worked on projects without UIScrollView) i read some about this issue and they suggested to use Gesture Recognizers, so i did.

My problem bow is this, i am able to drag it but it's delayed, i mean, i need to touch again the image to drag it after the longpress...

I have this code on my Longpress gesture:

-(void) handleLongPress:(UILongPressGestureRecognizer *)recognizer  { 
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            [img1 setFrame:CGRectMake(400, 385, 300, 300)];
            [scrollPaging setScrollEnabled:NO];
            [scrollPaging setUserInteractionEnabled:NO];
            break;

And this on the touchesMoved:

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

    location=[touch locationInView:self.view];
    img1.center=location;
        return;

thanks!

Upvotes: 3

Views: 1604

Answers (1)

Mikhail Vasilev
Mikhail Vasilev

Reputation: 1167

Try using UIPanGestureRecognizer to drag instead of touchesMoved. Also set delegate for both pan and long press recognizers and add this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; }

Upvotes: 1

Related Questions