問題描述
我想通過使用事件 swipeWithEvent 來實現在我的 webView 中向后和第四次導航的功能.但是,我不知道我應該如何使用它.
I want to implement the ability to navigate back and fourth in my webView by using the Event swipeWithEvent. However, I have no idea how I am suppose to use this.
我有一個主要的 webView,以及向后和第四個導航的兩種方法.這里的一個大問題是我不確定如何寫這個問題.我只需要知道如何識別我的 webView 上的滑動手勢并調用我的兩個方法.類似于 Safari、Google Chrome 和 Mozilla Firefox 所做的事情.謝謝.
I have one main webView, and two methods that navigate back and fourth. A big problem here is that I'm not exactly sure how to write this question. I just need to know how to recognize swipe gestures on my webView and call my two methods. Similar to what Safari, Google Chrome, and Mozilla Firefox do. Thanks.
編輯我已經實現了這些方法,這些方法可以讓我前后滑動.
EDIT I have implemented these methods which are suppose to allow me to swipe back and forward.
- (void)swipeWithEvent:(NSEvent *)event {
NSLog(@"Swipe With Event");
CGFloat x = [event deltaX];
//CGFloat y = [event deltaY];
if (x != 0) {
(x > 0) ? [self goBack:self] : [self goForward:self];
}
}
-(BOOL)recognizeTwoFingerGestures
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:@"AppleEnableSwipeNavigateWithScrolls"];
}
- (void)beginGestureWithEvent:(NSEvent *)event
{
if (![self recognizeTwoFingerGestures])
return;
NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:nil];
self.twoFingersTouches = [[NSMutableDictionary alloc] init];
for (NSTouch *touch in touches) {
[twoFingersTouches setObject:touch forKey:touch.identity];
}
}
- (void)endGestureWithEvent:(NSEvent *)event
{
if (!twoFingersTouches) return;
NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:nil];
// release twoFingersTouches early
NSMutableDictionary *beginTouches = [twoFingersTouches copy];
self.twoFingersTouches = nil;
NSMutableArray *magnitudes = [[NSMutableArray alloc] init];
for (NSTouch *touch in touches)
{
NSTouch *beginTouch = [beginTouches objectForKey:touch.identity];
if (!beginTouch) continue;
float magnitude = touch.normalizedPosition.x - beginTouch.normalizedPosition.x;
[magnitudes addObject:[NSNumber numberWithFloat:magnitude]];
}
// Need at least two points
if ([magnitudes count] < 2) return;
float sum = 0;
for (NSNumber *magnitude in magnitudes)
sum += [magnitude floatValue];
// Handle natural direction in Lion
BOOL naturalDirectionEnabled = [[[NSUserDefaults standardUserDefaults] valueForKey:@"com.apple.swipescrolldirection"] boolValue];
if (naturalDirectionEnabled)
sum *= -1;
// See if absolute sum is long enough to be considered a complete gesture
float absoluteSum = fabsf(sum);
if (absoluteSum < kSwipeMinimumLength) return;
// Handle the actual swipe
if (sum > 0)
{
[self goForward:self];
} else
{
[self goBack:self];
}
}
但是,這段代碼沒有做任何事情.它似乎根本沒有被調用.
However, this code isn't doing anything. It doesn't appear to be getting called at all.
推薦答案
對于現代操作系統(Lion 和更新的操作系統),這是我如何用 Y 做 X?"的問題之一.答案是不要使用 Y"的問題.
For modern OSes (Lion and newer), this is one of those "How do I do X with Y?" questions where the answer is "Don't use Y."
-swipeWithEvent:
用于實現 10.6 風格的觸控板滑動,屏幕上的內容不會隨著滑動而移動.大多數 Mac 觸控板不再配置為允許這種滑動;觸控板偏好面板中的在頁面之間滑動"首選項必須設置為用三個手指滑動"才能使用,這既不是默認設置,也不是用戶可以更改的常用設置.
-swipeWithEvent:
is used to implement 10.6-style trackpad swiping, where the content on the screen doesn't move with the swipe. Most Mac trackpads are not configured to allow this kind of swiping anymore; the "Swipe between pages" preference in Trackpad pref pane has to be set to "swipe with three fingers" for it to be available, and that's neither the default setting nor a common setting for users to change.
Lion 風格的流暢滑動"作為滾動事件出現.Xcode SDK 中的 PictureSwiper 示例項目是一個很好的起點,但作為概述,您可以這樣做:
Lion-style "fluid swipes" instead come in as scroll events. The PictureSwiper sample project in your Xcode SDK is a good place to start, but as an overview, here's what you do:
如果只需要支持10.8+
在歷史堆棧模式下使用 NSPageController
.
Use an NSPageController
in history stack mode.
如果需要支持 10.7
認真考慮僅支持 10.8+,至少對于此功能.手動實現它是一個巨大的混亂.別說我沒有警告你.
Seriously consider supporting only 10.8+, at least for this feature. Implementing it manually is a huge mess. Don't say I didn't warn you.
創建一個自定義視圖,它是您希望可滑動的任何視圖的超級視圖.
Create a custom view that is a superview to whatever views you want to be swipeable.
覆蓋 -wantsScrollEventsForSwipeTrackingOnAxis:
以返回相應軸的 YES
.如果您正在執行 Safari 風格的后退/前進導航,那就是 NSEventGestureAxisHorizo??ntal
.
Override -wantsScrollEventsForSwipeTrackingOnAxis:
to return YES
for the appropriate axis. If you're doing Safari-style back/forward navigation, that's NSEventGestureAxisHorizontal
.
深呼吸;這個太棒了.
在您的自定義視圖中覆蓋 -scrollWheel:
.您的覆蓋應該調用 -trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:
.粗略地說,衰減量閾值最小值是用戶可以向左滑動的數據瀏覽量,最大值是他們可以向右滑動的數據量.處理程序是一個在用戶滑動時重復調用的塊;它應該:
Override -scrollWheel:
in your custom view. Your override should call -trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:
. Roughly, the dampen amount threshold minimum is how many viewfuls of data the user can swipe to the left, and the maximum is how many they can swipe to the right. The handler is a block that's called repeatedly as the user swipes; it should:
- 在內容視圖后面放置一個
NSImageView
,其中包含您要返回/前進到的頁面的屏幕截圖. - 移動內容視圖以匹配用戶的動作.請注意,
gestureAmount
參數與阻尼量閾值一樣,是項目的(小數,可能是負數)數量;您必須將其乘以視圖寬度才能正確定位內容視圖. - 如果手勢階段是
NSEventPhaseEnded
,則評估gestureAmount
以確定用戶是否完成了手勢.如果他們沒有,將內容視圖動畫化回原位;如果他們這樣做了,請將內容視圖放回原位,不使用動畫并更新它以匹配屏幕截圖.
- Place an
NSImageView
with a screenshot of the page you're going back/forward to behind your content view. - Move the content view to match the user's movements. Note that the
gestureAmount
parameter is, like the dampen amount thresholds, a (fractional and possibly negative) number of items; you have to multiply it by the view width to correctly position the content view. - If the gesture phase is
NSEventPhaseEnded
, evaluate thegestureAmount
to determine if the user completed the gesture. If they didn't, animate the content view back into place; if they did, put the content view back in place with no animation and update it to match the screenshot.
如您所見,實際實現處理程序非常復雜,我什至沒有描述所有細節.即使有了所有這些細節,一個技術高超的程序員也可能需要花幾天時間才能做到這一點.10.7 SDK 中的 PictureSwiper 示例是一個很好的起點.(PictureSwiper 10.8 版本使用 NSPageController
.就像你應該做的那樣.)
As you can see, actually implementing the handler is very involved, and I haven't even described all of the specifics. Even with all of these details, a highly skilled programmer will probably have to spend a few days getting this just right. The PictureSwiper sample in the 10.7 SDK is a good place to start. (The 10.8 version of PictureSwiper uses NSPageController
. Just like you ought to do.)
這篇關于如何正確使用 swipeWithEvent 導航 webView,Obj-C的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!