我想通过使用事件 swipeWithEvent 来实现在我的 webView 中向后和第四次导航的能力。但是,我不知道我应该如何使用它。
我有一个主要的 webView,以及向后和第四个导航的两种方法。这里的一个大问题是我不确定如何写这个问题。我只需要知道如何识别我的 webView 上的滑动手势并调用我的两个方法。类似于 Safari、Google Chrome 和 Mozilla Firefox 所做的事情。谢谢。
编辑 我已经实现了这些方法,这些方法可以让我前后滑动。
- (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];
}
}
但是,这段代码没有做任何事情。它似乎根本没有被调用。