简体   繁体   中英

Stop touch moves if counter reaches 3

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM