Quiero implementar la capacidad de navegar hacia atrás y cuarto en mi vista web utilizando el evento swipeWithEvent. Sin embargo, no tengo idea de cómo se supone que debo usar esto.Cómo usar correctamente swipeWithEvent para navegar por una vista web, Obj-C
Tengo una vista web principal, y dos métodos que navegan hacia atrás y en cuarto lugar. Un gran problema aquí es que no estoy exactamente seguro de cómo escribir esta pregunta. Solo necesito saber cómo reconocer los gestos de deslizamiento en mi vista web y llamar a mis dos métodos. Similar a lo que hacen Safari, Google Chrome y Mozilla Firefox. Gracias.
EDIT He implementado estos métodos que se supone que me permiten deslizar hacia adelante y hacia atrás.
- (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];
}
}
Sin embargo, este código no está haciendo nada. No parece ser llamado en absoluto.
¿Podría cargar una aplicación de muestra donde '-swipeWithEvent:' no se llama? – Kentzo
Aquí. https://www.dropbox.com/s/sukkzvefwi836kr/swipeProject.zip?dl=1 – Josiah