Instantly share code, notes, and snippets.
Created
June 8, 2019 23:42
UIContextMenuInteraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| UIContextMenuInteraction *contextInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self]; | |
| [myCollectionViewCellOrWhatever addInteraction:contextInteraction]; | |
| */ | |
| #pragma mark - Context Menus | |
| #if TARGET_OS_UIKITFORMAC | |
| - (void)contextMenuInteractionWillPresent:(UIContextMenuInteraction *)interaction; | |
| { | |
| CWAPuzzlePreviewCell *cell = interaction.view; | |
| cell.layer.borderWidth = 5; | |
| cell.layer.borderColor = [UIColor systemBlueColor].CGColor; | |
| } | |
| - (void)contextMenuInteractionDidEnd:(UIContextMenuInteraction *)interaction; | |
| { | |
| CWAPuzzlePreviewCell *cell = interaction.view; | |
| cell.layer.borderWidth = 0; | |
| cell.layer.borderColor = [UIColor systemBlueColor].CGColor; | |
| } | |
| #endif | |
| - (nullable UIContextMenuConfiguration *)contextMenuInteraction:(nonnull UIContextMenuInteraction *)interaction configurationForMenuAtLocation:(CGPoint)location { | |
| CWAPuzzlePreviewCell *cell = interaction.view; | |
| CWAPuzzlePreviewActionButton *button = cell.actionButton; | |
| UIImageSymbolConfiguration *symcfg = [UIImageSymbolConfiguration configurationWithPointSize:18 weight:UIImageSymbolWeightBold]; | |
| UIContextMenuConfiguration *config = [UIContextMenuConfiguration configurationWithIdentifier:nil previewProvider:nil actionProvider:^UIMenu<UIAction *> * _Nullable(NSArray<UIMenuElement<UIAction *> *> * _Nonnull suggestedActions) { | |
| NSMutableArray *actions = [NSMutableArray array]; | |
| if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) | |
| { | |
| [actions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Open in New Window", nil) image:[UIImage systemImageNamed:@"plus.square.on.square" withConfiguration:symcfg] options:0 handler:^(__kindof UIAction * _Nonnull action) { | |
| [self openPuzzleInNewWindowForIndexPath:button.representedIndexPath]; | |
| }]]; | |
| } | |
| [actions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Share Puzzle", nil) image:[UIImage systemImageNamed:@"square.and.arrow.up" withConfiguration:symcfg] options:0 handler:^(__kindof UIAction * _Nonnull action) { | |
| [self sharePuzzleWorkForIndexPath:button.representedIndexPath withSender:button]; | |
| }]]; | |
| [actions addObject:[UIAction actionWithTitle:NSLocalizedString(@"See Solution", nil) image:[UIImage systemImageNamed:@"eyeglasses" withConfiguration:symcfg] options:0 handler:^(__kindof UIAction * _Nonnull action) { | |
| [self navigateToPuzzleAtIndexPath:button.representedIndexPath showSolutions:YES]; | |
| }]]; | |
| [actions addObject:[UIAction actionWithTitle:NSLocalizedString(@"Erase Work", nil) image:[UIImage systemImageNamed:@"delete.left.fill" withConfiguration:symcfg] options:UIActionOptionsDestructive handler:^(__kindof UIAction * _Nonnull action) { | |
| UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Really Erase?", nil) message:NSLocalizedString(@"This will erase your work and reset this puzzle to its original blank state. This cannot be undone.", nil) preferredStyle:UIAlertControllerStyleAlert]; | |
| [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Erase", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { | |
| [self erasePuzzleWorkForIndexPath:button.representedIndexPath]; | |
| // Distructive button tapped. | |
| [self dismissViewControllerAnimated:YES completion:^{ | |
| }]; | |
| }]]; | |
| [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
| // Distructive button tapped. | |
| [self dismissViewControllerAnimated:YES completion:^{ | |
| }]; | |
| }]]; | |
| [self presentViewController:alert animated:YES completion:nil]; | |
| }]]; | |
| return [UIMenu actionMenuWithTitle:@"" children:actions]; | |
| }]; | |
| return config; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment