1

I have an application with drag and drop a UIView feature. I should only let 3 drags and not more than that. How do I keep track of the number of drags? I tried incrementing a counter in touchesBegan(), but the counter got incremented on tapping the view also. I want it to be increased only when I drag the view. Also can you provide a snippet for dragging and dropping a small UIView into another view on the top. I don't know if I have used the right method.

My code:

(void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    [self.view setMultipleTouchEnabled:NO]; 
    UITouch *touch = [touches anyObject]; 
    if ([touch view] == option1View || [touch view] == option2View ||
        [touch view] == option3View ||[touch view] == option4View) { 
            CGRect frame = [[touch view] frame];
            if(counter == 1){
                frame.origin.x = 10;
                frame.origin.y = 90; 
            }
            [[touch view] setFrame:frame]; 

I incremented my counter in touched began. I checked that if condition for 3 cases of counter

Abhilasha
  • 929
  • 1
  • 17
  • 37
Preetha
  • 13
  • 4
  • Um...decrement it in touches ended? – borrrden Jul 24 '12 at 08:24
  • Hi,Welcome to SO. :) Could you please post some code here what you have written or at what point you failed to achieve it. – Abhilasha Jul 24 '12 at 08:27
  • - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { [self.view setMultipleTouchEnabled:NO]; UITouch *touch = [touches anyObject]; if ([touch view] == option1View || [touch view] == option2View || [touch view] == option3View ||[touch view] == option4View) { CGRect frame = [[touch view] frame]; if(counter == 1){ frame.origin.x = 10; frame.origin.y = 90; } [[touch view] setFrame:frame]; I incremented my counter in touched began. I checked that if condition for 3 cases of counter – Preetha Jul 24 '12 at 08:30

1 Answers1

0

Apple has a gesture recognizer exactly for dragging. It is UIPangestureRecognizer. It recognizes just pan gestures and not tap or something else.

Here is doc http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPanGestureRecognizer_Class/Reference/Reference.html

You can manage your pan/drag gestures easier then using touchesBegan() etc...

From Doc: UIGestureRecognizerStateEnded ends when all fingers are lifted.

You can check the state and increment your counter.

Abhilasha
  • 929
  • 1
  • 17
  • 37
Mert
  • 6,025
  • 3
  • 21
  • 33
  • Can u help me with the code pls.. Im new to xcode. I have 4 small views in the bottom and want the view i touch and drag to move to the top view. – Preetha Jul 24 '12 at 08:42
  • Here is a sample code http://stackoverflow.com/questions/6672677/how-to-use-uipangesturerecognizer-to-move-object-iphone-ipad It is old, but should give you the idea – Mert Jul 24 '12 at 08:52
  • And another sample code from apple http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460 – Mert Jul 24 '12 at 08:55