In snow leopard there is a gesture recognizer for swipe events:
- (void)swipeWithEvent:(NSEvent *)event {
CGFloat x = [event deltaX];
if (x != 0) {
(x > 0) ? [self goBack] : [self goForward];
}
}
Is there an equivalent that det开发者_JAVA百科ects two fingers swipe like the Safari app is doing for navigating pages in Lion?
I ended up extrapolating the useful info from this commit and implement it in my own project:
#define kSwipeMinimumLength 0.3
- (void)swipeWithEvent:(NSEvent *)event {
CGFloat x = [event deltaX];
//CGFloat y = [event deltaY];
if (x != 0) {
(x > 0) ? [self goBack] : [self goForward];
}
}
- (void)beginGestureWithEvent:(NSEvent *)event
{
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];
} else
{
[self goBack];
}
}
It's not 100% tested, but you get the idea.
Yes. See the AppKit release notes. (You should read them from end to end regardless.)
精彩评论