您的位置:首页 > 产品设计 > UI/UE

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(@"删除");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: