iOS开发UIMenuController 菜单栏控制

释放双眼,带上耳机,听听看~!

苹果官方提供的 菜单栏控制器的选择 (需要注意的就是让他成为第一响应者)
这里写图片描述

- (void)viewDidLoad {
[super viewDidLoad];
_wlLabel.userInteractionEnabled = YES;
[_wlLabel addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)longTap:(UILongPressGestureRecognizer *)tap {
//成为第一响应者
[self becomeFirstResponder];
if (tap.state == UIGestureRecognizerStateBegan) {
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(item)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(select)];
UIMenuItem *item3 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(shear)];
UIMenuController *menVC = [UIMenuController sharedMenuController];
[menVC setMenuItems:@[item1,item2,item3]];
[menVC setTargetRect:_wlLabel.frame inView:_wlLabel.superview];
[menVC setMenuVisible:YES animated:YES];
}
}
- (BOOL)canBecomeFirstResponder {
//明确该控件可以成为第一响应者
return YES;
}
//赋值
- (void)item {
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = _wlLabel.text;
}
//删除
- (void)select {
_wlLabel.text = nil;
}
//剪切
- (void)shear {
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = _wlLabel.text;
_wlLabel.text = @"";
}

人已赞赏
iOS文章

IOS之初见runLoop

2020-4-29 7:33:29

iOS文章

IOS 时间定时器

2020-4-29 8:48:37

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索