0

在我的应用程序中,我有一个文件查看器,它在 UIWebView 中显示多种类型的内容(图像、pdf、文本等)。

我有用于翻到下一页的滑动控件,只要图像小于 webview 并且不需要滚动,这些控件通常可以正常工作。这是代码:

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

但是,如果图像太大,滑动控件将不再起作用。我读过其他一些问题,人们尝试过类似的事情但没有成功。

我已经看到了对 UIWebView 进行子类化的建议,但这种方法也没有任何运气。

有没有办法将滑动控件添加到 UIWebView 将始终如一地工作?

4

1 回答 1

0

就我而言,我最终确实设法获得了这个工作的子类 UIWebView。我还创建了一个在执行方向滑动时调用的新委托。

这可能不是最好的解决方案,但它简单、可重用且有效。

下面是一些基本代码:

@interface SwipableWebView : UIWebView <UIGestureRecognizerDelegate>{
   id swipeableWebViewDelegate;
}
@property (nonatomic, retain) id <SwipableWebViewDelegate> swipeableWebViewDelegate;
@end


@implementation SwipableWebView{

}

@synthesize swipeableWebViewDelegate;

- (id)initWithCoder:(NSCoder *)aDecoder{
  self = [super initWithCoder:aDecoder];
  if (self) {
      UISwipeGestureRecognizer  * swipeRight = [[UISwipeGestureRecognizer     alloc]initWithTarget:self action:@selector(swipeRight:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeRight];
    swipeRight.delegate = self;

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


    UISwipeGestureRecognizer  * swipUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
    swipUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:swipUp];
    swipUp.delegate = self;

    UISwipeGestureRecognizer  * swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipeDown];
    swipeDown.delegate = self;
  }
  return self;
}



-(void)swipeLeft:(id)swipe{
  [swipeableWebViewDelegate webViewSwipeLeft:swipe];
}

-(void)swipeRight:(id)swipe{
  [swipeableWebViewDelegate webViewSwipeRight:swipe];
}

-(void)swipeUp:(id)swipe{
  [swipeableWebViewDelegate webViewSwipeUp:swipe];
}

-(void)swipeDown:(id)swipe{
  [swipeableWebViewDelegate webViewSwipeDown:swipe];
}

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

@end



@protocol SwipableWebViewDelegate <NSObject>
 -(void)webViewSwipeLeft:(id)swipe;
 -(void)webViewSwipeRight:(id)swipe;
 -(void)webViewSwipeUp:(id)swipe;
 -(void)webViewSwipeDown:(id)swipe;
@end
于 2013-01-25T15:08:40.897 回答