在单击 uicollectionviewcell 时,我想使用与菜单栏相同的外观,如下所示:
但是,我想写 move 而不是 cut
可能吗?我看到了一些答案——但他们都要求实现我自己的 UIActionSheet——但我希望它看起来像一个菜单而不是一个操作表
可能吗?
在单击 uicollectionviewcell 时,我想使用与菜单栏相同的外观,如下所示:
但是,我想写 move 而不是 cut
可能吗?我看到了一些答案——但他们都要求实现我自己的 UIActionSheet——但我希望它看起来像一个菜单而不是一个操作表
可能吗?
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
longPressGesture.minimumPressDuration=0.4;
[longPressGesture setDelegate:self];
[self.collectionCell addGestureRecognizer:longPressGesture];
- (void) showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Cut" action:@selector(copyAction:)];
UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(pasteAction)];
CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
[menuController setMenuItems:[NSArray arrayWithObjects: menuItem1, menuItem1,nil]];
[menuController setTargetRect:CGRectMake(location.x, location.y, 0, 0) inView:[gestureRecognizer view]];
[menuController setMenuVisible:YES animated:YES];
[self becomeFirstResponder];
}
}
如果其他人需要:
添加到 ViewDidLoad
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editMe:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Move" action:@selector(moveMe:)];
UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"DeleteMe" action:@selector(deletePlate:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:menuItem,menuItem2,menuItem3, nil]];
添加委托方法
-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action == @selector(editMe:) || action == @selector(moveMe:) || action == @selector(deleteMe:))
return YES;
return NO;
}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
将方法添加到自定义 UICollectionViewCell。例如:
-(void)editMe:(UIMenuController *)menuController{
}