UIMenuItem 模仿扣 4000 扣,微信聊天记录中长按转发,复制,粘贴等功能
2016-07-20 09:37
555 查看
简单的介绍一个小控件基本用法,具体功能就不写了,根据个人喜好添加功能
(1).在-viewDidLoad()中添加长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureAction:)];
longPressGesture.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPressGesture];
(2).长按手势相应事件
//长按手势
-(void)longPressGestureAction:(UILongPressGestureRecognizer *)longPressGesture
{
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
[self.view becomeFirstResponder];
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyAction)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(cutAction)];
UIMenuItem *item3 = [[UIMenuItem alloc]initWithTitle:@"转发" action:@selector(transmitAction)];
UIMenuItem *item4 = [[UIMenuItem alloc]initWithTitle:@"收藏" action:@selector(collectAction)];
UIMenuItem *item5 = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(deleteAction)];
UIMenuController *menu =[UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:item1,item2,item3,item4,item5,nil]];
[menu setTargetRect:self.view.bounds inView:self.view];
[menu setMenuVisible:YES animated:YES];
[menu setArrowDirection:UIMenuControllerArrowDown];
;
}
}
(3). 必须响应事件的两个条件
//必须带的条件一
-(BOOL)canBecomeFirstResponder{
return YES;
}
//条件二
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action == @selector(copyAction)||action == @selector(cutAction)||action == @selector(transmitAction)||action == @selector(collectAction)||action == @selector(deleteAction)) {
return YES;
}else{
return NO; //默认系统菜单项
}
}
(4).添加菜单栏中各个功能
-(void)copyAction{
NSLog(@"复制");
}
-(void)cutAction{
NSLog(@"剪切");
}
-(void)transmitAction{
NSLog(@"转发");
}
-(void)collectAction{
NSLog(@"收藏");
}
-(void)deleteAction{
NSLog(@"删除");
}
(1).在-viewDidLoad()中添加长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureAction:)];
longPressGesture.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPressGesture];
(2).长按手势相应事件
//长按手势
-(void)longPressGestureAction:(UILongPressGestureRecognizer *)longPressGesture
{
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
[self.view becomeFirstResponder];
UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyAction)];
UIMenuItem *item2 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(cutAction)];
UIMenuItem *item3 = [[UIMenuItem alloc]initWithTitle:@"转发" action:@selector(transmitAction)];
UIMenuItem *item4 = [[UIMenuItem alloc]initWithTitle:@"收藏" action:@selector(collectAction)];
UIMenuItem *item5 = [[UIMenuItem alloc]initWithTitle:@"删除" action:@selector(deleteAction)];
UIMenuController *menu =[UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:item1,item2,item3,item4,item5,nil]];
[menu setTargetRect:self.view.bounds inView:self.view];
[menu setMenuVisible:YES animated:YES];
[menu setArrowDirection:UIMenuControllerArrowDown];
;
}
}
(3). 必须响应事件的两个条件
//必须带的条件一
-(BOOL)canBecomeFirstResponder{
return YES;
}
//条件二
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action == @selector(copyAction)||action == @selector(cutAction)||action == @selector(transmitAction)||action == @selector(collectAction)||action == @selector(deleteAction)) {
return YES;
}else{
return NO; //默认系统菜单项
}
}
(4).添加菜单栏中各个功能
-(void)copyAction{
NSLog(@"复制");
}
-(void)cutAction{
NSLog(@"剪切");
}
-(void)transmitAction{
NSLog(@"转发");
}
-(void)collectAction{
NSLog(@"收藏");
}
-(void)deleteAction{
NSLog(@"删除");
}
相关文章推荐
- UIProgressView 进度表
- APUE 8-1 fork函数实例
- APUE 7-13 longjmp对各类对象的影响
- UIButton超出UIView边界不可点击
- APUE 7-9 进行命令处理程序的典型骨架部分
- UISwitch 开关状态
- UIButton超出UIView边界不可点击
- APUE 7-4 将所有命令行参数回显到标准输出
- APUE 7-3 终止处理程序实例
- Vue.js学习笔记(2)vue-router
- django model meta类定义abstract = True
- models.DateTimeField(auto_now_add=True)与models.DateTimeField(auto_now=True)
- 374. Guess Number Higher or Lower
- 【Hibernate】getHibernateTemplate.find()和session.createQuery()方法总结
- UIView,UIWindow,CALayer
- HUE配置hadoop
- 357. Count Numbers with Unique Digits
- UIImage切圆角 - 优化
- lucene 4.X 中的域缓存(FieldCache)和DocValue
- iOS中的UILabel自动计算高度和换行