在我的应用程序中,我有一个文件查看器,可以在UIWebView中显示多种类型的内容(图像,PDF,文本等)。
我有滑动控件以翻页到下一页,只要图像小于webview并且不需要滚动,这些通常都能正常工作。这是代码:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self;
[webView addGestureRecognizer:swipeRight];
但是,如果图像太大,则滑动控件不再起作用。 我读过其他一些问题,人们尝试过类似的事情,但没有成功。
我见过有关子类化UIWebView的建议,但这种方法也没有运气。
有没有办法将滑动控件添加到可以一致工作的UIWebView?
答案 0 :(得分: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