时间:2020-10-29 11:18:07 | 栏目:iOS代码 | 点击:次
Xcode7.2,iOS9.2环境下
尝试在CollectionViewCell内部,添加LongPress手势,显示UIMenuController。
@implementation CollectionViewCell //继承自UICollectionViewCell UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; longPress.minimumPressDuration = 1; [self addGestureRecognizer:longPress];//添加长按手势 //实现长按方法 - (void)longPress:(UILongPressGestureRecognizer *)gesture { switch (gesture.state) { case UIGestureRecognizerStateBegan: { [self becomeFirstResponder];<span style="color: rgb(255, 0, 0);">//必须有!</span> UIMenuController *menu = [UIMenuController sharedMenuController]; UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"保存" action:@selector(savePic)]; menu.menuItems = [NSArray arrayWithObject:item1]; [menu setTargetRect:self.scView.frame inView:self.scView.superview]; [menu setMenuVisible:YES animated:YES]; } break; default: break; } } <span style="color: rgb(0, 128, 0);">//必须实现。注意不是- (BOOL)becomeFirstResponder方法</span> - (BOOL)canBecomeFirstResponder { return YES; }
实现以上代码,就可以在长按cell时显示出UIMenuController。但是有一个问题就是,当我滑动CollectionView时,menu并没有第一时间隐藏。
所以,可以在你需要menu隐藏的方法内部。实现以下代码。
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:NO];