转载 http://blog.csdn.net/iukey/article/details/7430984
UIWebView 实现翻页功能
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];
- typedef enum {
- UISwipeGestureRecognizerDirectionRight = 1 << 0,
- UISwipeGestureRecognizerDirectionLeft = 1 << 1,
- UISwipeGestureRecognizerDirectionUp = 1 << 2,
- UISwipeGestureRecognizerDirectionDown = 1 << 3
- } UISwipeGestureRecognizerDirection;