1.注冊長按手勢(我自定義了一個Label,在Label中實現(xiàn))
self.userInteractionEnabled = true;
UILongPressGestureRecognizer * recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressEvent:)];
[self addGestureRecognizer:recognizer];
-(void)longPressEvent:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];//一定要寫
UIMenuController * menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:self.bounds inView:self];
[menuController setMenuVisible:YES animated:YES];
}
}
2.設(shè)置可為第一響應(yīng)(別寫錯了)
- (BOOL)canBecomeFirstResponder{
return true;
}
3.實現(xiàn)系統(tǒng)相關(guān)方法
- (void)copy:(id)sender{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:self.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return action == @selector(copy:);
}
系統(tǒng)有對應(yīng)復(fù)制、粘貼夸研、剪切等代理方法,我們只要實現(xiàn)方法就行了含末,記得在canPerformAction中根據(jù)需要添加即可
3.莫名閃退原因(如果你按上面寫了但還是會莫名閃退服协,請看下面)
檢查當(dāng)前界面所屬ViewController中有沒有你定義為inputView的屬性,有請換個名字!!!!!
Over~