UIPangestureRecognizer

UIPangestureRecognizer

我试图通过单击“保存”按钮关闭UIPanGestureRecognizer。然后,我试图通过单击“编辑”按钮重新打开此UIPanGestureRecognizer。我可以用以下代码找出如何关闭它们:

- (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;

    }
}
}
}


但是,我在设置为YES的代码行中收到错误。

这是我完整的代码集,以供参考:

@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


我想知道如何通过单击“保存” UIPanGestureRecognizer专门关闭UIBarButton,然后单击“编辑” UIPanGestureRecognizer来关闭UIBarButton

最佳答案

如果在IB中添加了平移手势,则可以创建出口,并在该手势实例上使用enabled属性来启用和禁用手势(简单)。

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

关于ios - 如何使用两个单独的UIBarButtonItems禁用/启用UIPanGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33602956/

10-13 03:06