【问题标题】:Action sheet is not getting dismissed on clicking outside the popup在弹出窗口外单击时不会关闭操作表
【发布时间】:2015-04-14 10:19:42
【问题描述】:

在 ios 7.0 中,我只想在用户在弹出框之外按下时关闭该操作表。我在按钮单击时显示该操作表并使用此代码尝试在 actionsheet.window 上获得一些点击手势

-(IBAction)buttonClicked:(id)sender{
    NSLog(@"button clicked");
    NSLog(@"action sheet ");
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil                                                         delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Rename",@"Delete",@"Cancel" ,nil];


    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        }
    }
    //  [actionSheet setBackgroundColor:[UIColor whiteColor]];
    [actionSheet showInView:self.view];


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
    tap.cancelsTouchesInView = NO;
    [actionSheet.window addGestureRecognizer:tap];

    [actionSheet.window addGestureRecognizer:tap];

}

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"in tap out");
    CGPoint p = [gestureRecognizer locationInView:actionSheet];
    if (p.y < 0) {
        NSLog(@" p.y < 0 ");
    }
}

但是我没有得到这个点击手势。请帮我解决这个问题。

【问题讨论】:

    标签: ios objective-c iphone uiactionsheet uitapgesturerecognizer


    【解决方案1】:

    您好,这是我在您点击外部时关闭操作表的解决方案

    首先创建一个视图来处理点击手势和操作表

    @implementation MyClass
    {
        UIActionSheet *_myActionSheet;
        UIView *_myTapView;
    }
    

    关于动作表委托方法

    - (void)didPresentActionSheet:(UIActionSheet *)actionSheet
    {
        [_myTapView removeFromSuperview];
        _myTapView = [[UIView alloc] initWithFrame:actionSheet.superview.bounds];
        _myTapView.backgroundColor = [UIColor clearColor];
    
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
        tap.cancelsTouchesInView = NO;
        [_myTapView addGestureRecognizer:tap];
    
        [actionSheet.superview insertSubview:_myTapView belowSubview:actionSheet];
    }
    

    并处理点击手势

    - (void)tapOut:(UIGestureRecognizer *)gestureRecognizer
    {
    //dismiss the action sheet here
        [_myActionSheet dismissWithClickedButtonIndex:<index> animated:YES];
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试在其他 actionSheet.window 上添加一个轻触。

      请尝试在self.superView上添加轻触

      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
      tap.cancelsTouchesInView = NO; 
      [self.superview addGestureRecognizer:tap];
      //OR
      [actionSheet.superview addGestureRecognizer:tap];
      

      像这样,你可以根据视图层次尝试不同的东西。

      更新 1

      尝试将 Actionsheet 子类化并在它自己喜欢的类中定义 Tapgesture。

      -(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
          CGPoint p = [gestureRecognizer locationInView:self];
          if (p.y < 0) { // They tapped outside
              [self dismissWithClickedButtonIndex:0 animated:YES];
          }
      }
      
      -(void) showInView:(UITabBar *)view {
          [super showFromTabBar:view];
      
          // Capture taps outside the bounds of this alert view
          UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
          tap.cancelsTouchesInView = NO; // So that legit taps on the table bubble up to the tableview
          [self.superview addGestureRecognizer:tap];
      }
      

      享受编码!

      【讨论】:

      • 是的,我什至尝试过.. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)]; tap.cancelsTouchesInView = 否; tap.numberOfTapsRequired = 1; actionSheet.superview.userInteractionEnabled = YES; [actionSheet.superview addGestureRecognizer:tap];但没有进入那个方法
      【解决方案3】:

      以下适用于 UIActionSheet 的子类

       actionSheet = [[UIActionSheet alloc] initWithTitle:nil                                                         delegate:self
                                       cancelButtonTitle:@"Cancel"
                                  destructiveButtonTitle:nil
                                       otherButtonTitles:@"Rename",@"Delete" ,nil];
      
      
      for (UIView *subview in actionSheet.subviews) {
          if ([subview isKindOfClass:[UIButton class]]) {
              UIButton *button = (UIButton *)subview;
              [button setBackgroundColor:[UIColor whiteColor]];
              [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
      
              [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
              [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
          }
      }
      //  [actionSheet setBackgroundColor:[UIColor whiteColor]];
      [actionSheet showInView:self.view];
      

      【讨论】:

      • 但我没有从标签栏显示操作表我正在使用 showInView 我尝试添加 [actionSheet.superView addGestureRecognizer:tap]
      • 我认为问题在于操作表将您的整个视图与透明视图重叠,这就是为什么您没有获得点击手势的原因。
      • 是的,那么当用户在操作表之外点击时我们如何关闭它
      • 你能帮我解决这个问题吗?
      • 我尝试了但没有找到任何解决您问题的方法
      【解决方案4】:

      此代码可帮助您关闭操作表

      [self dismissWithClickedButtonIndex:0 animated:YES];
      

      按钮索引“0”是取消按钮的按钮索引。

      【讨论】:

      • 当用户触摸操作表以外的任何地方而不是按钮单击时,我想关闭工作表
      • 是的,这样就可以在你的 tapout 函数中使用这个代码。
      • 当我点击屏幕时,tapout 函数没有被调用。这就是确切的问题
      • 你能帮我解决这个问题吗?
      猜你喜欢
      • 2012-12-21
      • 2021-11-21
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多