【问题标题】:Longpress inside actionsheet options在操作表选项中长按
【发布时间】:2015-05-25 10:17:43
【问题描述】:

是否可以在操作表的按钮内使用 UILongPressGestureRecognizer?

如果我触摸或长按,我应该在操作表内做不同的事情吗?

谢谢

【问题讨论】:

    标签: ios objective-c xcode


    【解决方案1】:

    是的,你可以

    UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"title" delegate:(id)self cancelButtonTitle:@"ok" destructiveButtonTitle:@"option" otherButtonTitles:nil, nil];
    
        [action showInView:self.view];
    
        UILongPressGestureRecognizer *longtaped = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(recog)];
    
        [action addGestureRecognizer:longtaped];
    

    它会工作得很好 干杯

    【讨论】:

    • 不起作用...我希望操作表的每个按钮都有长按手势
    • 仍然没有,如果我用该代码长按仍然调用:“actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex”但不是长按功能...
    • 我在此处发布之前运行该代码,这将尝试使用相同的方法实现,并且不要在您的项目中实现“actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex”方法....
    【解决方案2】:

    要么创建您的自定义视图,其行为类似于 ActionSheet。还可以尝试在下面提到的代码中将 actionsheet 委托设置为 nil。

    -(void)AddactionSheet{
    
        UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"title" delegate:(id)self cancelButtonTitle:@"ok" destructiveButtonTitle:@"option" otherButtonTitles:nil, nil];
    
        [action showInView:self.view];
    
    
        for(UIView *v in [action subviews])
        {
            if([v isKindOfClass:[UIButton class]] )
            {
                //((UIButton*)v).backgroundColor = [UIColor redColor];   // change button color
    
                UILongPressGestureRecognizer *longtaped = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(recog)];
    
                [((UIButton*)v) addGestureRecognizer:longtaped];
    
            }
        }
    
    }
    
    -(void)recog{
        NSLog(@"Longpressed");
    
    }
    

    此代码将为您工作 如果您想使用不同的长按添加不同的按钮,则必须添加“n”个手势 干杯

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      相关资源
      最近更新 更多