如何使用两个单独的 UIBarButtonItem 禁用/启用 UIPanGestureRecognizer?
Posted
技术标签:
【中文标题】如何使用两个单独的 UIBarButtonItem 禁用/启用 UIPanGestureRecognizer?【英文标题】:How to disable/enable a UIPanGestureRecognizer with two separate UIBarButtonItems? 【发布时间】:2015-11-09 05:19:36 【问题描述】:我正在尝试通过单击“保存”按钮关闭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
我想知道如何通过点击我的“保存”UIBarButton
来专门关闭UIPanGestureRecognizer
,然后通过点击我的“编辑”UIBarButton
来关闭UIPanGestureRecognizer
。
【问题讨论】:
您可以启用/禁用 PanGestureRecognizer,只需将其启用属性设置为 YES 或 NO。 如果m文件中设置了手势可以这样做吗 是的。 “启用”只是每个手势识别器的一个属性,参见 developer.apple.com/library/ios/documentation/UIKit/Reference/…>。 这是我用来禁用平移手势属性的代码- (IBAction)saveButton:(id)sender buttonCount ++; if (buttonCount > 0) for (_buttonField in self.view.subviews) UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)]; panner.enabled = NO;
知道为什么这没有触发吗?
【参考方案1】:
如果您在 IB 中添加了 Pan 手势,那么您可以创建 outlet 并使用手势实例上的 enabled 属性来启用和禁用手势,很简单。
panGestureRecognizer.enabled = NO; (or)
panGestureRecognizer.enabled = YES;
【讨论】:
这确实有道理,但是当我将手势添加到按钮并将其称为禁用时,它在模拟器中不起作用。 我试图通过在 .h 中声明它来解决这个问题,但我无法弄清楚以上是关于如何使用两个单独的 UIBarButtonItem 禁用/启用 UIPanGestureRecognizer?的主要内容,如果未能解决你的问题,请参考以下文章