将滑动手势添加到 epub ios 应用
Add swipe gesture to epub ios app
所以我正在使用基于 epub reader 应用程序的代码,我正在尝试修改它以使滑动手势正常工作。我只需要左右手势来向前或向后更改页面,并检查 epub 的结束或开始的边界。我确定了切换页面的代码,并将它们附加到调用 java 脚本的下一个和上一个按钮函数来执行此操作。加载java脚本和WKWebView的函数在- (void)loadView
函数中,函数在下面,我的尝试是*UISwipeGestureRecognizer
被注释掉的声明
- (void)loadView {
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
// Notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(onEPubSettingsDidChange:)
name:kSDKLauncherEPubSettingsDidChange object:nil];
// Create the web view. The choice of web view type is based on the existence of the WKWebView
// class, but this could be decided some other way.
// The "no optimize" RequireJS option means that the entire "readium-shared-js" folder must be copied in to the OSX app bundle's "scripts" folder! (including "node_modules" subfolder, which is populated when invoking the "npm run prepare" build command) There is therefore some significant filesystem / size overhead, but the benefits are significant too: no need for the WebView to fetch sourcemaps, and to attempt to un-mangle the obfuscated Javascript during debugging.
// However, the recommended development-time pattern is to invoke "npm run build" in order to refresh the "build-output" folder, with the RJS_UGLY environment variable set to "false" or "no". This way, the RequireJS single/multiple bundle(s) will be in readable uncompressed form.
//NSString* readerFileName = @"reader_RequireJS-no-optimize.html";
//NSString* readerFileName = @"reader_RequireJS-multiple-bundles.html";
NSString* readerFileName = @"reader_RequireJS-single-bundle.html";
if ([WKWebView class] != nil) {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.allowsInlineMediaPlayback = YES;
config.mediaPlaybackRequiresUserAction = NO;
// Configure a "readium" message handler, which is used by host_app_feedback.js.
WKUserContentController *contentController = [[WKUserContentController alloc] init];
[contentController addScriptMessageHandler:self name:@"readium"];
config.userContentController = contentController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
m_webViewWK = webView;
webView.hidden = YES;
webView.scrollView.bounces = NO;
webView.allowsBackForwardNavigationGestures = YES;
[self.view addSubview:webView];
// RDPackageResourceConnection looks at corePaths and corePrefixes in the following
// query string to determine what core resources it should provide responses for. Since
// WKWebView can't handle file URLs, the web server must provide these resources.
NSString *url = [NSString stringWithFormat:
@"%@%@?"
@"corePaths=readium-shared-js_all.js,readium-shared-js_all.js.map,epubReadingSystem.js,host_app_feedback.js,sdk.css&"
@"corePrefixes=readium-shared-js",
m_package.rootURL,
readerFileName];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
//attempted swipe gestures, maybe create seperate function not sure if this is the best method
/*UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self.view;
[webView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self.view;
[webView addGestureRecognizer:swipeLeft];/*
}
else {
UIWebView *webView = [[UIWebView alloc] init];
m_webViewUI = webView;
webView.delegate = self;
webView.hidden = YES;
webView.scalesPageToFit = YES;
webView.scrollView.bounces = NO;
webView.allowsInlineMediaPlayback = YES;
webView.mediaPlaybackRequiresUserAction = NO;
[self.view addSubview:webView];
NSURL *url = [[NSBundle mainBundle] URLForResource:readerFileName withExtension:nil];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//attempted swipe gestures, maybe create seperate function
/*UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
//swipeRight.delegate = self;
[webView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//swipeLeft.delegate = self;
[webView addGestureRecognizer:swipeLeft];*/
}
}
非常感谢任何帮助,我已经向原始开发人员寻求帮助,但他们还没有回应。我以为我会得到别人的意见。
谢谢
找到解决办法。我在 UIWebview
和 WKWebview
上使用了 UISwipeGestureRecogniser
委托滑动操作,然后我创建了处理这些操作的函数。尽管此代码特定于此框架,但它可能与某些人相关。我的代码如下:
-(void) viewDidLoad{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
//swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//swipeRight.delegate = self;
[m_webViewUI addGestureRecognizer:swipeRight];
[m_webViewWK addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
//swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
//swipeLeft.delegate = self;
[m_webViewUI addGestureRecognizer:swipeLeft];
[m_webViewWK addGestureRecognizer:swipeLeft];}
然后我将后退和前进手势映射到翻页:
-(void)swipeLeftAction:(UISwipeGestureRecognizer *) swipe{
[self executeJavaScript:@"ReadiumSDK.reader.openPageNext()" completionHandler:nil];
NSLog(@"%s","Swipe Left");}
-(void)swipeRightAction:(UISwipeGestureRecognizer *) swipe{
[self executeJavaScript:@"ReadiumSDK.reader.openPagePrev()" completionHandler:nil];
NSLog(@"%s","Swipe Right");
边界检查由框架 java 脚本处理。
所以我正在使用基于 epub reader 应用程序的代码,我正在尝试修改它以使滑动手势正常工作。我只需要左右手势来向前或向后更改页面,并检查 epub 的结束或开始的边界。我确定了切换页面的代码,并将它们附加到调用 java 脚本的下一个和上一个按钮函数来执行此操作。加载java脚本和WKWebView的函数在- (void)loadView
函数中,函数在下面,我的尝试是*UISwipeGestureRecognizer
被注释掉的声明
- (void)loadView {
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
// Notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(onEPubSettingsDidChange:)
name:kSDKLauncherEPubSettingsDidChange object:nil];
// Create the web view. The choice of web view type is based on the existence of the WKWebView
// class, but this could be decided some other way.
// The "no optimize" RequireJS option means that the entire "readium-shared-js" folder must be copied in to the OSX app bundle's "scripts" folder! (including "node_modules" subfolder, which is populated when invoking the "npm run prepare" build command) There is therefore some significant filesystem / size overhead, but the benefits are significant too: no need for the WebView to fetch sourcemaps, and to attempt to un-mangle the obfuscated Javascript during debugging.
// However, the recommended development-time pattern is to invoke "npm run build" in order to refresh the "build-output" folder, with the RJS_UGLY environment variable set to "false" or "no". This way, the RequireJS single/multiple bundle(s) will be in readable uncompressed form.
//NSString* readerFileName = @"reader_RequireJS-no-optimize.html";
//NSString* readerFileName = @"reader_RequireJS-multiple-bundles.html";
NSString* readerFileName = @"reader_RequireJS-single-bundle.html";
if ([WKWebView class] != nil) {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.allowsInlineMediaPlayback = YES;
config.mediaPlaybackRequiresUserAction = NO;
// Configure a "readium" message handler, which is used by host_app_feedback.js.
WKUserContentController *contentController = [[WKUserContentController alloc] init];
[contentController addScriptMessageHandler:self name:@"readium"];
config.userContentController = contentController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
m_webViewWK = webView;
webView.hidden = YES;
webView.scrollView.bounces = NO;
webView.allowsBackForwardNavigationGestures = YES;
[self.view addSubview:webView];
// RDPackageResourceConnection looks at corePaths and corePrefixes in the following
// query string to determine what core resources it should provide responses for. Since
// WKWebView can't handle file URLs, the web server must provide these resources.
NSString *url = [NSString stringWithFormat:
@"%@%@?"
@"corePaths=readium-shared-js_all.js,readium-shared-js_all.js.map,epubReadingSystem.js,host_app_feedback.js,sdk.css&"
@"corePrefixes=readium-shared-js",
m_package.rootURL,
readerFileName];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
//attempted swipe gestures, maybe create seperate function not sure if this is the best method
/*UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
swipeRight.delegate = self.view;
[webView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = self.view;
[webView addGestureRecognizer:swipeLeft];/*
}
else {
UIWebView *webView = [[UIWebView alloc] init];
m_webViewUI = webView;
webView.delegate = self;
webView.hidden = YES;
webView.scalesPageToFit = YES;
webView.scrollView.bounces = NO;
webView.allowsInlineMediaPlayback = YES;
webView.mediaPlaybackRequiresUserAction = NO;
[self.view addSubview:webView];
NSURL *url = [[NSBundle mainBundle] URLForResource:readerFileName withExtension:nil];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//attempted swipe gestures, maybe create seperate function
/*UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
//swipeRight.delegate = self;
[webView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//swipeLeft.delegate = self;
[webView addGestureRecognizer:swipeLeft];*/
}
}
非常感谢任何帮助,我已经向原始开发人员寻求帮助,但他们还没有回应。我以为我会得到别人的意见。
谢谢
找到解决办法。我在 UIWebview
和 WKWebview
上使用了 UISwipeGestureRecogniser
委托滑动操作,然后我创建了处理这些操作的函数。尽管此代码特定于此框架,但它可能与某些人相关。我的代码如下:
-(void) viewDidLoad{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
//swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//swipeRight.delegate = self;
[m_webViewUI addGestureRecognizer:swipeRight];
[m_webViewWK addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
//swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
//swipeLeft.delegate = self;
[m_webViewUI addGestureRecognizer:swipeLeft];
[m_webViewWK addGestureRecognizer:swipeLeft];}
然后我将后退和前进手势映射到翻页:
-(void)swipeLeftAction:(UISwipeGestureRecognizer *) swipe{
[self executeJavaScript:@"ReadiumSDK.reader.openPageNext()" completionHandler:nil];
NSLog(@"%s","Swipe Left");}
-(void)swipeRightAction:(UISwipeGestureRecognizer *) swipe{
[self executeJavaScript:@"ReadiumSDK.reader.openPagePrev()" completionHandler:nil];
NSLog(@"%s","Swipe Right");
边界检查由框架 java 脚本处理。