简单的介绍一个小控件基本用法,具体功能就不写了,根据个人喜好添加功能
(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(@"删除");
}