UIMenuController和UIMenuItem,即iOS剪贴板

1、剪贴板菜单使用原理说明

苹果默认只实现 UITextView,UITextField,webView三个控件的剪贴板,一般控件的剪贴板不但未实现,而且还被都被禁用了,所以,如果需要使用其它控件的剪贴板菜单,首先需要使能其剪贴板菜单,即在自定义控件中实现两个简单的方法,方法如下:

响应方法

- (BOOL)canBecomeFirstResponder{

returnYES;

}

//"反馈"关心的功能,即放出你需要的功能,比如你要放出copy,你就返回YES,否则返回NO

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{

if (action ==@selector(copy:)){

returnYES;

}

else if (action ==@selector(paste:)){

return YES;

}

else if (action ==@selector(cut:)){

returnNO;

}

else if(action ==@selector(select:)){

returnNO;

}

else if (action ==@selector(delete:)){

returnNO;

}

return NO;

}

然后,需要将剪贴板菜单添加到自定义控件中(此时已经可以弹出剪贴板菜单),方法如下:

-(void)attachTapHandler{

self.userInteractionEnabled =YES; //用户交互的总开关

[self becomeFirstResponder];//自定义控件设置为第一响应者

UITapGestureRecognizer *touch = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(handleTap:)];

touch.numberOfTapsRequired =1;

[selfaddGestureRecognizer:touch];

[touch release];

}

//响应点击事件(如果不添加菜单项,则使用菜单中默认菜单项)

-(void)handleTap:(UIGestureRecognizer*) recognizer{

[selfbecomeFirstResponder];

UIMenuController *menu = [UIMenuControllersharedMenuController];

[menu setTargetRect:self.frameinView:self.superview];

[menu setMenuVisible:YESanimated:YES];

}

//如果需要自定义菜单项,则点击响应事件如下(此时只弹出自定义菜单项)

-(void)handleTap:(UILongPressGestureRecognizer *)longRecognizer

{

if (longRecognizer.state==UIGestureRecognizerStateBegan) {

[self becomeFirstResponder];

UIMenuController *menu=[UIMenuController sharedMenuController];

UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyItemClicked:)];

UIMenuItem *resendItem = [[UIMenuItem alloc] initWithTitle:@"转发" action:@selector(resendItemClicked:)];

[menu setMenuItems:[NSArray arrayWithObjects:copyItem,resendItem,nil]];

[menu setTargetRect:self.bounds inView:self];

[menu setMenuVisible:YES animated:YES];

}

}

最后,需要实现剪贴板菜单中菜单项的响应方法,如下:

-(void)copy:(id)sender{


UIPasteboard *pboard = [UIPasteboard generalPasteboard];

pboard.string = self.text;

}



- (void)paste:(id)sender{


self.textAlignment =UITextAlignmentRight;

UIPasteboard *pboard = [UIPasteboard generalPasteboard];

self.text = [NSString stringWithFormat:@"粘贴内容:%@",pboard.string];

NSLog(@"pboard.string : %@",pboard.string);

}




2、剪贴板菜单使用举例,使用自定义的菜单项

1).添加longpress事件

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

[self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];

}

return self;

}

2).处理长按事件

-(void)longTap:(UILongPressGestureRecognizer *)longRecognizer

{

if (longRecognizer.state==UIGestureRecognizerStateBegan) {

[self becomeFirstResponder];

UIMenuController *menu=[UIMenuController sharedMenuController];

UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyItemClicked:)];

UIMenuItem *resendItem = [[UIMenuItem alloc] initWithTitle:@"转发" action:@selector(resendItemClicked:)];

[menu setMenuItems:[NSArray arrayWithObjects:copyItem,resendItem,nil]];

[menu setTargetRect:self.bounds inView:self];

[menu setMenuVisible:YES animated:YES];

}

}

3).实现默认方法

#pragma mark 处理action事件

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{

if(action ==@selector(copyItemClicked:)){

return YES;

}else if (action==@selector(resendItemClicked:)){

return YES;

}

return [super canPerformAction:action withSender:sender];

}

#pragma mark 实现成为第一响应者方法

-(BOOL)canBecomeFirstResponder{

return YES;

}

4).处理item点击事件

#pragma mark method

-(void)resendItemClicked:(id)sender{

NSLog(@"转发");

//通知代理

}

-(void)copyItemClicked:(id)sender{

NSLog(@"复制");

// 通知代理

}


3、UITableView两种实现剪贴板菜单的方法

1)、自定义剪贴板菜单

不再多说,在本文1中已经说明

2)、iOS 5.0以后苹果为UITableView开放了三个方法,用于简单实现长按UITableView cell所需的菜单,方法如下:

-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{


return YES;

}



-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

if (action == @selector(copy:)) {

return YES;

}


return NO;

}



-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (action == @selector(copy:)) {

[UIPasteboard generalPasteboard].string = cell.textLabel.text;

}

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页