首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UILongPressGestureRecognizer不工作

UILongPressGestureRecognizer不工作
EN

Stack Overflow用户
提问于 2013-02-04 20:03:48
回答 3查看 6.7K关注 0票数 3

我想要检测UIWebView tap的UILongPressGestureRecognizer -并按住..such键,当我长按近3秒时,下面的if条件应该是True then only

每次..does不检查longPressGesture时间时,循环中都会继续执行if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture ),但不是working....it ...

就连我也试过这种情况..

if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration> 3 )

不是working..where,我在犯错误..

代码语言:javascript
复制
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{


UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] init];

longGesture.numberOfTapsRequired = 1;
longGesture.numberOfTouchesRequired = 1;
longGesture.minimumPressDuration = 3
;
longGesture.delegate = self;
// longGesture.allowableMovement = 50;

[self.webView addGestureRecognizer:longGesture];



if (navigationType == UIWebViewNavigationTypeLinkClicked  && longGesture )
{
    // Call your custom actionsheet and use the requestURL to do what you want :)


    UIActionSheet *sheet = [[UIActionSheet alloc]
                            initWithTitle:@" OPTIONS "
                            delegate:self
                            cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                            otherButtonTitles:nil];


    [sheet addButtonWithTitle:@"Open"];
    [sheet addButtonWithTitle:@"Copy"];

    // Set cancel button index to the one we just added so that we know which one it is in delegate call
    // NB - This also causes this button to be shown with a black background
    sheet.cancelButtonIndex = sheet.numberOfButtons-1;

    [sheet showInView:webView];
    return NO;
  }
EN

回答 3

Stack Overflow用户

发布于 2013-02-04 20:14:57

您应该启用同步手势识别,因为UIWebView本身设置了一些识别器,您的识别器将被跳过:在代码中添加以下内容

代码语言:javascript
复制
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
票数 3
EN

Stack Overflow用户

发布于 2013-02-04 20:48:44

你没有为你的手势识别器设置目标动作,是吗?

代码语言:javascript
复制
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)];

设置一个目标动作将让你得到通知,无论手势是否触发!我将从这种方法开始,首先检查是否调用了"longPressGestureUpdated“方法。

试试下面的定义:

代码语言:javascript
复制
    longPGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)];
    longPressGesture.numberOfTouchesRequired = 1;
    longPressGesture.delegate = self;
    longPressGesture.cancelsTouchesInView = NO;

(并按照MilKyWaY的建议启用同步手势识别)

票数 2
EN

Stack Overflow用户

发布于 2013-02-05 14:49:18

代码语言:javascript
复制
- (void)viewDidLoad
{ 
  [super viewDidLoad];
  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
  longPress.numberOfTouchesRequired = 1;
  [self.view addGestureRecognizer:longPress];
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
  if (gesture.state == UIGestureRecognizerStateBegan) 
  {
   UIActionSheet *sheet = [[UIActionSheet alloc]
                        initWithTitle:@" OPTIONS "
                        delegate:self
                        cancelButtonTitle:nil
                        destructiveButtonTitle:nil
                        otherButtonTitles:nil];


  [sheet addButtonWithTitle:@"Open"];
  [sheet addButtonWithTitle:@"Copy"];

  // Set cancel button index to the one we just added so that we know which one it is in delegate call
  // NB - This also causes this button to be shown with a black background
  sheet.cancelButtonIndex = sheet.numberOfButtons-1;

  [sheet showInView:webView];
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14686406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档