现在的位置: 首页 > 综合 > 正文

UIWebView 实现翻页功能

2018年07月13日 ⁄ 综合 ⁄ 共 1116字 ⁄ 字号 评论关闭

UIWebView倒是经常用,不过翻页功能没用过,因为还没写过电子书之类的应用,最近心血来潮写了个电子书的框架,这样就可以量产电子书应用了,哈哈。可是要实现翻页功能时发现UIwebView竟然接收不了touch事件,老郁闷了。网上有大牛的hack方法,不过审核貌似通不过。组后发现苹果官方给我们提供了专门的api,通过 UISwipeGestureRecognizer 来实现。

直接上代码吧:

  1. UISwipeGestureRecognizer  *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage)];  
  2.    swipeRight.direction=UISwipeGestureRecognizerDirectionRight;  
  3.    [webView addGestureRecognizer:swipeRight];  
  4.    [swipeRight release];  
  5.      
  6.    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)];  
  7.    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;  
  8.    [webView addGestureRecognizer:swipeLeft];  
  9.    [swipeLeft release];  

一看代码应该就明白了。苹果官方给我们的手势有4个,分别对应上下左右:

  1. typedef enum {  
  2.     UISwipeGestureRecognizerDirectionRight = 1 << 0,  
  3.     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,  
  4.     UISwipeGestureRecognizerDirectionUp    = 1 << 2,  
  5.     UISwipeGestureRecognizerDirectionDown  = 1 << 3  
  6. } UISwipeGestureReco

转自:http://blog.csdn.net/iukey/article/details/7430984

抱歉!评论已关闭.