UIWebView 实现翻页功能

openkk 12年前

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

直接上代码吧:

    UISwipeGestureRecognizer  *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage)];           swipeRight.direction=UISwipeGestureRecognizerDirectionRight;           [webView addGestureRecognizer:swipeRight];           [swipeRight release];                      UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)];           swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;           [webView addGestureRecognizer:swipeLeft];           [swipeLeft release];  
一看代码应该就明白了。苹果官方给我们的手势有4个,分别对应上下左右:
typedef enum {      UISwipeGestureRecognizerDirectionRight = 1 << 0,      UISwipeGestureRecognizerDirectionLeft  = 1 << 1,      UISwipeGestureRecognizerDirectionUp    = 1 << 2,      UISwipeGestureRecognizerDirectionDown  = 1 << 3  } UISwipeGestureRecognizerDirection;
来自:http://blog.csdn.net/iukey/article/details/7430984