Tôi muốn triển khai khả năng điều hướng ngược lại và thứ tư trong webView của mình bằng cách sử dụng Event swipeWithEvent. Tuy nhiên, tôi không có ý tưởng làm thế nào tôi giả sử để sử dụng này.Cách sử dụng đúng swipeWithEvent để điều hướng một webView, Obj-C
Tôi có một webView chính và hai phương pháp điều hướng ngược lại và thứ tư. Một vấn đề lớn ở đây là tôi không chắc chắn làm thế nào để viết câu hỏi này. Tôi chỉ cần biết cách nhận ra cử chỉ vuốt trên webView của tôi và gọi hai phương thức của tôi. Tương tự như Safari, Google Chrome và Mozilla Firefox. Cảm ơn.
EDIT Tôi đã triển khai các phương pháp này giả sử cho phép tôi vuốt lại và tiến.
- (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];
}
}
Tuy nhiên, mã này không hoạt động. Nó dường như không được gọi là tất cả.
Bạn có thể tải lên một ứng dụng mẫu trong đó '-swipeWithEvent:' không được gọi? – Kentzo
Ở đây. https://www.dropbox.com/s/sukkzvefwi836kr/swipeProject.zip?dl=1 – Josiah