#pragma mark - UIView添加
- (void)showPopupView:(UIButton*)button {
button.selected= !button.selected;
//必須要有,如果要UIMenuController顯示
[self becomeFirstResponder];
_menuController= [UIMenuControllersharedMenuController];
UIMenuItem*menuitem_01 = [[UIMenuItemalloc]initWithTitle:@"delete"action:@selector(deleteAction)];
UIMenuItem*menuitem_02 = [[UIMenuItemalloc]initWithTitle:@"copy"action:@selector(copyAction)];
UIMenuItem*menuitem_03 = [[UIMenuItemalloc]initWithTitle:@"cancel"action:@selector(cancelAction)];
_menuController.menuItems= [NSArrayarrayWithObjects:menuitem_01, menuitem_02, menuitem_03,nil];
[_menuControllersetTargetRect:CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width,30)inView:self.view];
[_menuControllersetMenuVisible:YESanimated:YES];
}
//必須要有,如果要UIMenuController顯示
-(BOOL)canBecomeFirstResponder {
return YES;
}
//監聽自己的定義事件,是return YES;否return NO即移除系統;
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action ==@selector(deleteAction) || action ==@selector(copyAction) || action ==@selector(cancelAction)) {
returnYES;
}
returnNO;
}
#pragma mark -UITableViewCell添加
一、使用UITableViewDelegate方法無需子類化
前提:我們假設一個控制器中有一個tableView,并完成了tableView的基本設置。
0.在viewDidLoad中:給menu添加一個舉報的Item
UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"舉報" action:@selector(report:)];
UIMenuController *menu=[UIMenuController sharedMenuController];
[menu setMenuItems: @[testMenuItem]];
[menu update];//必須要更新
1.是否顯示Menu
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
2.是否該顯示系統菜單選項并調(就是到底顯示系統的哪幾項)
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
//顯示一個系統的,一個自定義的
return (action == @selector(copy:) || action == @selector(report:));
}
3.根據action來判斷具體要什么執行動作
-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action == @selector(copy:)) {
NSLog(@"copy...");
}
else if(action == @selector(report:)){
NSLog(@"report...");
}
}
二、使用長按手勢加子類化(大多數用到cell都會把它子類化的)
0.在cell子類化初始化方法中或者在數據源方法返回cell的時候都可以加上手勢
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[cell addGestureRecognizer:longPress];
1.實現長按方法
-(void)longPress:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
YDSonCommentCell *cell = (YDSonCommentCell *)recognizer.view;
[cell becomeFirstResponder];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"舉報"action:@selector(report:)];
UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"拷貝"action:@selector(copy:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:report,copy, nil]];
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
}
}
2.cell子類化實現兩個方法
//只有成為第一響應者時menu才會彈出
-(BOOL)canBecomeFirstResponder
{
return YES;
}
//是否可以接收某些菜單的某些交互操作
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
return (action == @selector(copy:) || action == @selector(report:));
}
//實現這兩個方法來執行具體操作
-(void)copy:(id)sender{
[UIPasteboard generalPasteboard].string=self.comment.Content;
}
-(void)report:(id)sender{
NSLog(@"舉報啦");
}