简体   繁体   English

iOS webview swipegesturerecognizer和缩放

[英]iOS webview swipegesturerecognizer and zoom

In my app i have a webview with left and right swipegesturerecognizers. 在我的应用程序中,我有一个带左右swipegesturerecognizers的webview。 The problem is when i zoom in webview. 问题是当我放大webview时。 It seems that swipegesturerecognizers are disturbing the webview scrollview delegate and webview zoom works bad. 似乎swipegesturerecognizers扰乱webview scrollview委托和webview缩放工作不好。 How can i do this correctly? 我怎么能正确地做到这一点?

- (void)viewDidLoad
{
  [super viewDidLoad];
  UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
  swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
  swipeRight.delegate = self;
  [webView addGestureRecognizer:swipeRight];

  UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
  swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  swipeLeft.delegate = self;
  [webView addGestureRecognizer:swipeLeft];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  return YES;
}

- (void)swipeRightAction:(id)ignored
{
  NSLog(@"Swipe Right");
  //add Function
}

- (void)swipeLeftAction:(id)ignored
{
  NSLog(@"Swipe Left");
  //add Function
}

I solved it changing above delegate method with this one: 我解决了它改变上面的委托方法与这一个:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
   return YES;
}

You've already answered your own question, but I'm going to add this answer for people who find this in the future: 您已经回答了自己的问题,但我将为将来发现此问题的人添加此答案:

If you only need to support iOS 8+, you can use WKWebView from the new WebKit framework, which replaces UIWebView . 如果您只需要支持iOS 8+,则可以使用新的WebKit框架中的WKWebView ,它取代了UIWebView WKWebView supports back/forward swipes out of the box (you'll need to set its allowsBackForwardNavigationGestures property to YES (the default is NO )). WKWebView支持WKWebView后退/前进滑动(您需要将其allowsBackForwardNavigationGestures属性设置为YES (默认值为NO ))。 This means you won't need to add your own gesture recognisers for back/forward swipes. 这意味着您无需为后退/前进滑动添加自己的手势识别器。

It behaves just like Safari, which means it nicely handles the distinction between a forward/back swipe and a zoom gesture. 它的行为就像Safari一样,这意味着它可以很好地处理前进/后退滑动和缩放手势之间的区别。

I solved it changing above delegate method with this one: 我解决了它改变上面的委托方法与这一个:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM