单击弹出窗口外的操作表不会被解雇

时间:2015-04-14 10:19:43

标签: ios objective-c iphone uiactionsheet uitapgesturerecognizer

在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 ");
    }
}

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

4 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

此代码可帮助您解除操作表

[self dismissWithClickedButtonIndex:0 animated:YES];

按钮索引&#39; 0&#39;是取消按钮的按钮索引。

答案 2 :(得分:0)

您可以尝试在不同的actionSheet.window上添加一个tapgesture。

请尝试在self.superView

上添加tapgesture
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];
}

享受编码!!

答案 3 :(得分:0)

以下内容适用于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];
相关问题