码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——项目总结OC篇&iOS 长按事件菜单的实现(剪切版)

时间:2015-09-21 01:30:02      阅读:1338      评论:0      收藏:0      [点我收藏+]

标签:

iOS 长按事件菜单的实现(剪切版)

 

 
一:简单实现菜单
添加长按手势
   
    [self.label addGestureRecognizer:[[UILongPressGestureRecognizer alloc] 
       
initWithTarget:self action:@selector(labelClick)]];
 
实现手势方法
 1 - (void)labelClick
 2 {
 3     // 获得菜单
 4     UIMenuController *menu = [UIMenuController sharedMenuController];
 5    
 6     // 菜单最终显示的位置
 7     CGRect rect = CGRectMake(100, 100, 100, 100);
 8     [menu setTargetRect:rect inView:self.label];
 9     /*
10      targetRect:menuController指向的矩形框
11      targetView:targetRect以targetView的左上角为坐标原点
12      */
13    
14     // 显示菜单
15     [menu setMenuVisible:YES animated:YES];
16    
17     /*
18      得通过第一响应者,来告诉MenuController它内部应该显示什么内容
19      */
20 }
21  

 

以下两个方法是必须实现的,用来设置第一响应者和菜单显示的内容
 1 #pragma mark - 第一响应者 + UIMenuController
 2 /**
 3  * 说明控制器可以成为第一响应者
 4  */
 5 - (BOOL)canBecomeFirstResponder
 6 {
 7     return YES;
 8 }
 9 
10 /**
11  * 通过这个方法告诉UIMenuController它内部应该显示什么内容
12  * 返回YES,就代表支持action这个操作
13  */
14 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
15 {
16     if (action == @selector(cut:)
17         || action == @selector(copy:)
18         || action == @selector(paste:)) {
19         return YES;
20     }
21    
22     return NO;
23 }
 
最后实现菜单按钮点击方法
 1 - (void)cut:(UIMenuController *)menu
 2 {
 3     NSLog(@"%s %@", __func__, menu);
 4 }
 5 
 6 - (void)copy:(UIMenuController *)menu
 7 {
 8     NSLog(@"%s %@", __func__, menu);
 9 }
10 
11 - (void)paste:(UIMenuController *)menu
12 {
13     NSLog(@"%s %@", __func__, menu);
14 }
 
二:自定义菜单
 
同样添加手势
 
    [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
 

 

实现手势方法,这里注意里面添加Item的代码
 1 - (void)labelClick
 2 {
 3     // 让label成为第一响应者
 4     [self becomeFirstResponder];
 5    
 6     // 获得菜单
 7     UIMenuController *menu = [UIMenuController sharedMenuController];
 8    
 9     // 设置菜单内容
10     menu.menuItems = @[
11                        [[UIMenuItem alloc] initWithTitle:@"" action:@selector(ding:)],
12                        [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(reply:)],
13                        [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(warn:)]
14                        ];
15    
16     // 菜单最终显示的位置
17     [menu setTargetRect:self.bounds inView:self];
18    
19     // 显示菜单
20     [menu setMenuVisible:YES animated:YES];
21 }
22 实现设置第一响应者方法和设置菜单项的方法
23 #pragma mark - UIMenuController相关
24 /**
25  * 让Label具备成为第一响应者的资格
26  */
27 - (BOOL)canBecomeFirstResponder
28 {
29     return YES;
30 }
31 
32 /**
33  * 通过第一响应者的这个方法告诉UIMenuController可以显示什么内容
34  */
35 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
36 {
37     if ( (action == @selector(copy:) && self.text) // 需要有文字才能支持复制
38         || (action == @selector(cut:) && self.text) // 需要有文字才能支持剪切
39         || action == @selector(paste:)
40         || action == @selector(ding:)
41         || action == @selector(reply:)
42         || action == @selector(warn:)) return YES;
43    
44     return NO;
45 }

 

实现按钮点击方法
 1 #pragma mark - 监听MenuItem的点击事件
 2 - (void)cut:(UIMenuController *)menu
 3 {
 4     // 将label的文字存储到粘贴板
 5     [UIPasteboard generalPasteboard].string = self.text;
 6     // 清空文字
 7     self.text = nil;
 8 }
 9 
10 - (void)copy:(UIMenuController *)menu
11 {
12     // 将label的文字存储到粘贴板
13     [UIPasteboard generalPasteboard].string = self.text;
14 }
15 
16 - (void)paste:(UIMenuController *)menu
17 {
18     // 将粘贴板的文字赋值给label
19     self.text = [UIPasteboard generalPasteboard].string;
20 }
21 
22 - (void)ding:(UIMenuController *)menu
23 {
24     NSLog(@"%s %@", __func__, menu);
25 }
26 
27 - (void)reply:(UIMenuController *)menu
28 {
29     NSLog(@"%s %@", __func__, menu);
30 }
31 
32 - (void)warn:(UIMenuController *)menu
33 {
34     NSLog(@"%s %@", __func__, menu);
35 }
36  
37  

 

三:cell中的实战
在点击Cell方法中实现菜单的创建
 1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     // 取出cell
 4     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 5    
 6     UIMenuController *menu = [UIMenuController sharedMenuController];
 7    
 8     // 设置菜单内容
 9     menu.menuItems = @[
10                        [[UIMenuItem alloc] initWithTitle:@"" action:@selector(ding:)],
11                        [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(reply:)],
12                        [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(warn:)]
13                        ];
14    
15     // 显示位置
16     CGRect rect = CGRectMake(0, cell.height * 0.5, cell.width, 1);
17     [menu setTargetRect:rect inView:cell];
18    
19     // 显示出来
20     [menu setMenuVisible:YES animated:YES];
21 }

 

获得选中的项
 1 #pragma mark - 获得当前选中的评论
 2 - (XMGComment *)selectedComment
 3 {
 4     // 获得被选中的cell的行号
 5     NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
 6     NSInteger row = indexPath.row;
 7    
 8     // 获得评论数据
 9     NSArray *comments = self.latestComments;
10     if (indexPath.section == 0 && self.hotComments.count) {
11         comments = self.hotComments;
12     }
13    
14     return comments[row];
15 }
16  
17  

 

同上实现必须实现的饿方法
 1 #pragma mark - UIMenuController处理
 2 - (BOOL)canBecomeFirstResponder
 3 {
 4     return YES;
 5 }
 6 
 7 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
 8 {
 9     if (!self.isFirstResponder) { // 文本框弹出键盘, 文本框才是第一响应者
10         if (action == @selector(ding:)
11             || action == @selector(reply:)
12             || action == @selector(warn:)) return NO;
13     }
14    
15     return [super canPerformAction:action withSender:sender];
16 }
17  

 

最后实现按钮的点击方法
 1 - (void)ding:(UIMenuController *)menu
 2 {
 3     XMGLog(@"ding - %@ %@",
 4            self.selectedComment.user.username,
 5            self.selectedComment.content);
 6 }
 7 
 8 - (void)reply:(UIMenuController *)menu
 9 {
10     XMGLog(@"reply - %@ %@",
11            self.selectedComment.user.username,
12            self.selectedComment.content);
13 }
14 
15 - (void)warn:(UIMenuController *)menu
16 {
17     XMGLog(@"warn - %@ %@",
18            self.selectedComment.user.username,
19            self.selectedComment.content);
20 } 
问题:点击Cell可以实现弹出菜单,但是点击控制器的其他地方,比如其他文本框或者问题也出现
1:点击Cell位置做一个标记,判断是否标记是否设置,如果有责是点击Cell进来的,
 
 

iOS开发——项目总结OC篇&iOS 长按事件菜单的实现(剪切版)

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4824749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!