简体   繁体   中英

How to fire an alert / pop-up on swipe in Mac OS X app

How can I fire an alert in a Mac OS X App that occurs when a swipe or pinch happens. The alert will state the direction of the swipe and if a pinch then if it is a pinch in or a pinch out

Apple's Documentation on handling trackpad events is quite informative. You need to look at the swipeWithEvent:(NSEvent *)event method, and the magnifyWithEvent:(NSEvent *)event method. They give clear examples of what you can do with these methods and how to implement them. I made this really quickly, so this code has not been tested. But you would need to do something like this. I would suggest you read the entire article then create your own code.

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

    NSString *msg = @"";

    if (x != 0) {
        msg = (x > 0) ? @"Left Swipe" : @"Right Swipe";
    }
    if (y != 0) {
        msg = (y > 0)  ? @"Up Swipe" : @"Down Swipe";
    }

    [self displayWithMessage:msg];
}

- (void)magnifyWithEvent:(NSEvent *)event {
    NSString *msg = @"";
    if([event magnification] > 0) {
        msg = @"Pinch In";
    }
    else if ([event magnification] < 0) {
        msg = @"Pinch Out";
    }

    [self displayWithMessage:msg];

}
-(void) displayWithMessage:(NSString *)message {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setAlertStyle:NSInformationalAlertStyle];
    [alert setMessageText:@"Gesture Notification"];
    [alert setInformativeText:message];
    [alert runModal];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM