iOS收藏iosiOS开发技术

UIMenuController的使用简介

2016-07-10  本文已影响9884人  断剑

UIMenuController苹果官方文档

示例代码下载地址

1. UIMenuController简介

在很多应用中,当我们长按一段文字或者图片的时候会弹出一个菜单,我们通过这个菜单可以实现文字等的复制、剪切、删除以及各种操作。

示例图片

2. UIMenuController相关方法

+ (UIMenuController *)sharedMenuController
@property(nonatomic,getter=isMenuVisible) BOOL menuVisible;     // default is NO
//是否通过动画进行设置显示、隐藏
- (void)setMenuVisible:(BOOL)menuVisible animated:(BOOL)animated;

** 注意 ** 在显示menu之前,一点要确定为menu设置与其相关的显示位置

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

注意

- (void)update
@property(nonatomic, copy) NSArray <UIMenuItem *> *menuItems

@interface UIMenuItem : NSObject 
//创建UIMenuItem对象
- (instancetype)initWithTitle:(NSString *)title action:(SEL)action ;
@property(nonatomic,copy) NSString *title;
@property(nonatomic)      SEL       action;
typedef enum {
   UIMenuControllerArrowDefault,
   UIMenuControllerArrowUp,
   UIMenuControllerArrowDown,
   UIMenuControllerArrowLeft,
   UIMenuControllerArrowRight,
} UIMenuControllerArrowDirection;
UIMenuControllerWillShowMenuNotification
UIMenuControllerDidShowMenuNotification
UIMenuControllerWillHideMenuNotification
UIMenuControllerDidHideMenuNotification
UIMenuControllerMenuFrameDidChangeNotification

3.自定义控件的UIMenuController

//设置控件可以成为第一响应者,注意不是每个控件都可以成为第一响应者
- (BOOL)canBecomeFirstResponder;    // default is NO
/**
 *  设置控件能够执行那些具体操作
 *  @param action 具体操作
 *  @return YES:支持该操作
 */
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender;
// Allows an action to be forwarded to another target. By default checks -canPerformAction:withSender: to either return self, or go up the responder chain.
- (void)cut:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)copy:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)paste:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)select:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)selectAll:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)delete:(nullable id)sender NS_AVAILABLE_IOS(3_2);
- (void)makeTextWritingDirectionLeftToRight:(nullable id)sender NS_AVAILABLE_IOS(5_0);
- (void)makeTextWritingDirectionRightToLeft:(nullable id)sender NS_AVAILABLE_IOS(5_0);

//私有方法
   _promptForReplace:
   _transliterateChinese:
   _showTextStyleOptions:
   _define:
   _addShortcut:
   _accessibilitySpeak:
   _accessibilitySpeakLanguageSelection:
   _accessibilityPauseSpeaking:
   _share:
#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
#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];
}
上一篇 下一篇

猜你喜欢

热点阅读