如何在点击UIBarButtonItems时启用/禁用UIPanGestureRecognizer?

时间:2015-11-10 17:39:44

标签: ios objective-c uibarbuttonitem uipangesturerecognizer

我目前无法通过点击UIBarButtonItem来禁用/启用平移手势。目前我理解它的简单代码,但由于某种原因,代码没有被读取。到目前为止,这是我的代码:

- (IBAction)saveButton:(UIBarButtonItem*)saveRep {



buttonCount ++;
if (buttonCount > 0) {

        UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];


    for (_buttonField in self.view.subviews) {        

        panner.enabled=NO;


    }
}
}
- (IBAction)editButton:(UIBarButtonItem*)editRep {



buttonCount ++;
if (buttonCount > 0) {

    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];
    _draggedView = panner.view;

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

    }

}
}

当我点击saveButton时,我希望禁用panner,我已使用panner.enabled=NO;设置了但是,此代码无效!按下保存按钮后,平移器仍处于启用状态并正常工作。

然后我想点击editButton再次启用panner,我试图用panner.enable=YES;

最初我将这个平移手势识别器添加到我点击我的addRepButton时创建的UIButton。这是我的整套代码供参考:

@implementation FieldGoalChartViewController
{

}

-(void)viewDidLoad{
[super viewDidLoad];


}

-(void) viewWillAppear:(BOOL)animated
{

}

- (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];
touch.view.layer.borderColor = [UIColor whiteColor
                                ].CGColor;
}


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

}
- (IBAction)saveButton:(UIBarButtonItem*)saveRep {



buttonCount ++;
if (buttonCount > 0) {

    for (_buttonField in self.view.subviews) {

        UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];


        panner.enabled=NO;


    }
}
}
- (IBAction)editButton:(UIBarButtonItem*)editRep {



buttonCount ++;
if (buttonCount > 0) {

    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];
    _draggedView = panner.view;

    for (_buttonField in self.view.subviews) {
        panner.enabled = 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在被调用时没有被启用/禁用。

0 个答案:

没有答案