UIMenuController

1.自定义Label的UIMenuController

#import "ZZYMenuLabel.h"

@implementation ZZYMenuLabel
/**
 *  xib创建label时调用
 */
- (void)awakeFromNib
{
    [self setUp];
}
/**
 *  代码创建label时调用
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
         [self setUp];
    }
    return self;
}

- (void)setUp
{
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)]];
}

- (void)longPress
{
    NSLog(@"%s",__func__);

    //1.设置label为第一响应者
    //通过设置第一响应者UIMenuController可以获得支持哪些操作的信息,操作怎么处理
    [self becomeFirstResponder];

    //2.设置UIMenuController
    UIMenuController * menu = [UIMenuController sharedMenuController];

    //当长按label的时候,这个方法会不断调用,menu就会出现一闪一闪不断显示,需要在此处进行判断
    if (menu.isMenuVisible)return;
    //自定义 UIMenuController

    UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(myCut:)];
    UIMenuItem * item2 = [[UIMenuItem alloc]initWithTitle:@"粘贴" action:@selector(myPaste:)];
    menu.menuItems = @[item1,item2];

    [menu setTargetRect:self.bounds inView:self];
//  [menu setTargetRect:self.frame inView:self.superview];

    [menu setMenuVisible:YES animated:YES];

}
#pragma mark - 对控件权限进行设置
/**
 *  设置label可以成为第一响应者
 *
 *  @注意:不是每个控件都有资格成为第一响应者
 */
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
/**
 *  设置label能够执行那些具体操作
 *
 *  @param action 具体操作
 *
 *  @return YES:支持该操作
 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//    NSLog(@"%@",NSStringFromSelector(action));

    if(action == @selector(cut:) || action == @selector(copy:) || action == @selector(myCut:)|| action == @selector(myPaste:)) return YES;
    return NO;
}

#pragma mark - 方法的实现
//- (void)cut:(id)sender
//{
//    
//    NSLog(@"%@",sender);
//    
//}

- (void)myCut:(UIMenuController *) menu
{
    NSLog(@"%s---%@",__func__,menu);
    //复制文字到剪切板
    [self copy:menu];
    //清空文字
    self.text = nil;

}

- (void)cut:(UIMenuController *)menu
{
    //复制文字到剪切板
    [self copy:menu];
    //清空文字
    self.text = nil;

}

- (void)copy:(UIMenuController *)menu
{
    //当没有文字的时候调用这个方法会崩溃
     if (!self.text) return;
    //复制文字到剪切板
    UIPasteboard * paste = [UIPasteboard generalPasteboard];
    paste.string = self.text;

}

- (void)myPaste:(UIMenuController *)menu
{
    //将剪切板文字赋值给label
    UIPasteboard * paste = [UIPasteboard generalPasteboard];
    self.text = paste.string;
}
@end

2.UITableViewCell的使用

#import "ZZYTableViewCell.h"

@implementation ZZYTableViewCell

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

@end

#import "ZZYTableViewController.h"
#import "ZZYTableViewCell.h"

@interface ZZYTableViewController ()
@property (nonatomic, weak) ZZYTableViewCell * selectCell;
@end

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当menucontroller显示,点击不同的cell时为什么会显示。
//    menuController的显示依赖于第一响应者,当点击另外的cell时,当前cell取消第一响应者状态,menucontroller自动消失
    UIMenuController * menu = [UIMenuController sharedMenuController];
    NSLog(@"%d",menu.isMenuVisible);
    //防止点击多次创建
    if (menu.isMenuVisible)
    {
        [menu setMenuVisible:NO animated:YES];
    }
    else
    {

    ZZYTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        self.selectCell = cell;

    [cell becomeFirstResponder];

    UIMenuItem * item0 = [[UIMenuItem alloc]initWithTitle:@"分享" action:@selector(share:)];
    UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"评论" action:@selector(comment:)];
    UIMenuItem * item2 = [[UIMenuItem alloc]initWithTitle:@"点赞" action:@selector(praise:)];
    menu.menuItems = @[item0,item1,item2];

    [menu setTargetRect:CGRectMake(0, cell.frame.size.height * 0.5, cell.frame.size.width, cell.frame.size.height) inView:cell];

    [menu setMenuVisible:YES animated:YES];
    }
}


- (void)share:(UIMenuController *)menu
{
    NSLog(@"%@",self.selectCell.textLabel.text);
}

- (void)comment:(UIMenuController *)menu
{

}

- (void)praise:(UIMenuController *)menu
{

}

//防止拖动tableView时产生的BUG
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
}

3.设置位置

/**
 *  设置menu显示的位置信息
 *
 *  @param targetRect menu需要显示的矩形区域
 *  @param targetView targetRect会以targetView的左上角为坐标原点进行显示
 */
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;

你可能感兴趣的:(UIMenuController)