gpt4 book ai didi

objective-c - swipeWithEvent 相当于 Lion

转载 作者:行者123 更新时间:2023-12-03 17:27:35 25 4
gpt4 key购买 nike

在雪豹中有一个用于滑动事件的手势识别器:

- (void)swipeWithEvent:(NSEvent *)event {
CGFloat x = [event deltaX];

if (x != 0) {
(x > 0) ? [self goBack] : [self goForward];
}
}

是否有类似的功能可以检测两根手指的滑动,就像 Safari 应用程序在 Lion 中导航页面一样?

最佳答案

我最终从this commit中推断出了有用的信息。并在我自己的项目中实现它:

    #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];
}


}

它没有经过 100% 测试,但您已经明白了。

关于objective-c - swipeWithEvent 相当于 Lion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6874047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com