UILongPressGestureRecognizer stop handle without stop touching

前端 未结 2 458
轻奢々
轻奢々 2021-01-05 11:28

I\'m using UILongPressGestureRecognizer class to handle if one item is being selected.

The logic is as follows: User press during 1 second an item (UIView subclass).

2条回答
  •  抹茶落季
    2021-01-05 12:08

    There are two solutions in my mind.

    1. For animating uiview, please wrote a new class which is inherited from the UIView class and implement the touch delegates instead of writing the Gustures to handle animation(if the touch delegates are not triggering in the current class).

    2.I have successfully removed the UILongPressGestureRecognizer after triggered it once.

    Please refer the below code .ask me if you have any queries

    Steps I have Done

    I have added a UIView as "myView" to my main-view when main-view loads.

    I have given the Tag to the myView (you can give 1,2,3…etc) to differentiate the tapped view from the main-view subviews.

    Assigned the UILongPressGestureRecognizer gesture to myView and assigned target as "moveMe" method.

    When user Pressed the myView long, the "moveMe" method will trigger.

    Then I iterated the mainView Subviews with the condition Tag == 1

    I have removed the UILongPressGestureRecognizer from the subview.As we can know that Tagged 1 main-view subView is myView.

    So the NSLog(@"gesture removed"); and NSLog(@"moveMe"); will log in console only at one time.

    The NSLog(@"touchesBegan"); will trigger first instead of triggering the "moveMe" method.

    Then NSLog(@"touchesBegan"); will trigger always after removed the gesture . "moveMe" method will not trigger ever.

    Code

        - (void)viewDidLoad {    
            //Adding to UIView to main view when application is loading.
             UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];        
             myView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
              myView.tag = 1; //adding a tag to identify it.
            //Adding Long Press Gesture to the UIView.
            UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveMe:)];
            [myView addGestureRecognizer:myGesture];
            [myGesture release];
            myGesture = nil;   
           [self.view addSubview:myView];   
           [myView release];
            myView = nil;    
            [super viewDidLoad];
        }    
    
        //Method to trigger when user pressed long on the added UIView.
    
     -(void)moveMe:(id)sender
     { 
          for (UIView *subViews in [self.view subviews]) 
          { 
                if (subViews.tag == 1) { 
                     [subViews removeGestureRecognizer:sender];
                     NSLog(@"gesture removed");
                 }    
             }    
             NSLog(@"moveMe");
        }    
     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     {
         NSLog(@"touchesBegan");
     }
    

    or please refer Disable gesture recognizer iOS

提交回复
热议问题