Lozo
Lozo

Reputation: 79

How to disable/enable a UIPanGestureRecognizer with two separate UIBarButtonItems?

I am trying to turn off a UIPanGestureRecognizer with the click of a "Save" button. Then, I am trying to turn back on this UIPanGestureRecognizer with the click of an "Edit" button. I can figure out how to turn them off with the following code like this:

- (IBAction)saveButton:(id)sender {

buttonCount ++;
if (buttonCount > 0) {

    for (_buttonField in self.view.subviews) {
        _buttonField.gestureRecognizers = nil;

        _buttonField.layer.borderColor = [UIColor blackColor].CGColor;


    }
    }
    }

Yet, I have two problems. First of all, this is turning off all of the gesture recognizers, which I do not want to do. Secondly, I cannot figure out how to turn them back on. For this I have tried the following:

- (IBAction)editButton:(id)sender {

buttonCount ++;
if (buttonCount > 0) {

    if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {

    for (_buttonField in self.view.subviews) {
        _buttonField.gestureRecognizers = YES;

    }
}
}
}

However, I receive an error on the line of code that is set to YES.

Here is my complete set of code for reference:

@implementation FieldGoalChartViewController
{


}

-(void)viewDidLoad{
[super viewDidLoad];




}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
UITouch *aTouch = [touches anyObject];

CGRect buttonRect = self.buttonField.frame;


CGPoint point = [aTouch locationInView:self.buttonField.superview];



if (!CGRectContainsPoint(buttonRect, point)) {
        _buttonField.layer.borderColor = [UIColor blackColor].CGColor;
        _draggedView.layer.borderColor = [UIColor blackColor].CGColor;
    for (_buttonField in self.view.subviews) {
        _buttonField.layer.borderColor = [UIColor blackColor].CGColor;

    }

    }


    }

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateBegan ) {
    gesture.view.layer.borderColor = [UIColor whiteColor].CGColor;


    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Would you like to delete the selected rep(s)?"
                                  message:nil
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* deleteButton = [UIAlertAction
                                actionWithTitle:@"Delete"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)

                                   {
                                       for (_buttonField in self.view.subviews) {
                                           if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {

                                               [_buttonField removeFromSuperview];

                                           }
                                       }

                                    [alert dismissViewControllerAnimated:YES completion:nil];

                                }];
    UIAlertAction* cancelButton = [UIAlertAction
                               actionWithTitle:@"Cancel"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                                   {






                                   [alert dismissViewControllerAnimated:YES completion:nil];

                               }];

    [alert addAction:deleteButton];
    [alert addAction:cancelButton];

    [self presentViewController:alert animated:YES completion:nil];
    }
    }




    - (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

    {

    panner.view.layer.borderColor = [UIColor whiteColor].CGColor;


   _draggedView = panner.view;

    CGPoint offset = [panner translationInView:_draggedView.superview];
    CGPoint center = _draggedView.center;
    _draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
    _draggedView.layer.masksToBounds =YES;
    _buttonField.layer.borderWidth = 3.0f;




    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:_draggedView.superview];

    }

    }



-(void)buttonTouched:(UIButton*)sender forEvent:(id)tap {
NSSet *touches = [tap allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor whiteColor
                                ].CGColor;
}


-(void)doubleTapped:(UIButton*)sender forEvent:(id)twoTaps {
NSSet *touches = [twoTaps allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor blackColor].CGColor;

}
- (IBAction)saveButton:(id)sender {

buttonCount ++;
if (buttonCount > 0) {

    for (_buttonField in self.view.subviews) {
        _buttonField.gestureRecognizers = nil;

        _buttonField.layer.borderColor = [UIColor blackColor].CGColor;


    }
    }
  }

- (IBAction)editButton:(id)sender {

buttonCount ++;
if (buttonCount > 0) {

    if ([[UIColor colorWithCGColor:_buttonField.layer.borderColor] isEqual:[UIColor whiteColor]]) {

    for (_buttonField in self.view.subviews) {
        _buttonField.gestureRecognizers = YES;

    }
}
}
}


- (IBAction)addRepButton:(UIBarButtonItem *)newRep {

self.labelCounter++;

buttonCount ++;
if (buttonCount > 0 )
{

    _buttonField = [[UIButton alloc]initWithFrame:CGRectMake(300, 300, 28, 28)];
    [_buttonField setTitle:[NSString stringWithFormat:@"%i", self.labelCounter]forState:UIControlStateNormal];
    [_buttonField setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _buttonField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    _buttonField.userInteractionEnabled = YES;
    _buttonField.layer.cornerRadius = 14;
    _buttonField.layer.borderColor = [UIColor blackColor].CGColor;
    _buttonField.layer.borderWidth = 3.0f;
    _buttonField.titleLabel.font = [UIFont boldSystemFontOfSize:13];
    [_buttonField setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _buttonField.layer.backgroundColor = [UIColor blackColor].CGColor;
    _buttonField.layer.masksToBounds = YES;

    //Pan gesture declared in button
    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];
    [_buttonField addGestureRecognizer:panner];

    //Long Press gesture declared in button
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.buttonField addGestureRecognizer:longPress];

    //Touch down inside declared in button
    [self.buttonField addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];

    //Double Tap inside declared in button
    [self.buttonField addTarget:self action:@selector(doubleTapped:forEvent:) forControlEvents:UIControlEventTouchDownRepeat];

    [self.view addSubview:(_buttonField)];




}


}







@end

I want to know how to specifically turn off the UIPanGestureRecognizer with the click of my "Save" UIBarButton, and then turn the UIPanGestureRecognizer with the click of my "Edit" UIBarButton.

Upvotes: 0

Views: 1147

Answers (1)

harsha yarabarla
harsha yarabarla

Reputation: 506

If you added Pan gesture in IB then you can create outlet and use the enabled property on the instance of the gesture to enable and disable the gesture, simple.

panGestureRecognizer.enabled = NO; (or)
panGestureRecognizer.enabled = YES;

Upvotes: 1

Related Questions