UIMenuController类详解

前言:

官方网址https://developer.apple.com/documentation/uikit/uimenucontroller?language=objc

概述:

UIMenuController是苹果提供的用于剪切、复制、粘贴、单选、全选和删除等命令的菜单界面。UIMenuController对象可以包含一组UIMenuItem对象(菜单项),并能在现有的视图上显示这些UIMenuItem对象。每个UIMenuItem对象都有自己的标题和动作方法。每个ios应用只有一个UIMenuController对象,当应用要显示该对象时,要先为它设置一组UIMenuItem对象,然后设置显示位置(矩形区域),最后设置为可见。值得注意的是,要显示UIMenuController对象,必须满足一个条件:显示UIMenuController对象的UIView对象必须是当前UIWindow对象的第一响应对象,即如果要将某个UIView对象设置为第一响应对象,就必须覆盖该对象的canBecomeFirstResponder方法。

属性方法:

获取菜单控制器

@property(class, nonatomic, readonly) UIMenuController *sharedMenuController;

+ (UIMenuController *)sharedMenuController;

显示和隐藏菜单

/*立即显示和隐藏菜单,而不需要动画效果,默认为NO*/
@property(nonatomic,getter=isMenuVisible) BOOL menuVisible; 
/*显示和隐藏菜单,设置动画效果*/
- (void)setMenuVisible:(BOOL)menuVisible animated:(BOOL)animated;

定位

/* 设置菜单显示的位置信息,targetRect是菜单要显示的矩形区域,targetRect会以targetView的左上角为坐标原点进行显示*/
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;
/*菜单的边框,可用此属性来调整菜单以外的其它用户界面对象*/
@property(nonatomic,readonly) CGRect menuFrame;
/*菜单箭头指向的方向,默认为UIMenuControllerArrowDefault*/
@property(nonatomic) UIMenuControllerArrowDirection arrowDirection NS_AVAILABLE_IOS(3_2); 
/*菜单箭头指向的方向枚举*/
typedef NS_ENUM(NSInteger, UIMenuControllerArrowDirection) {
    /*基于屏幕位置的上下*/
    UIMenuControllerArrowDefault, 
    /*菜单上方*/
    UIMenuControllerArrowUp NS_ENUM_AVAILABLE_IOS(3_2),
     /*菜单下方*/
    UIMenuControllerArrowDown NS_ENUM_AVAILABLE_IOS(3_2),
     /*菜单左边*/
    UIMenuControllerArrowLeft NS_ENUM_AVAILABLE_IOS(3_2),
     /*菜单右边*/
    UIMenuControllerArrowRight NS_ENUM_AVAILABLE_IOS(3_2),
} __TVOS_PROHIBITED;

更新菜单

- (void)update; 

自定义菜单项

/*自定义菜单项,默认为零*/
@property(nullable, nonatomic,copy) NSArray *menuItems NS_AVAILABLE_IOS(3_2); 

监听的状态:

/*菜单显示之前*/
UIKIT_EXTERN NSNotificationName const UIMenuControllerWillShowMenuNotification __TVOS_PROHIBITED;
/*菜单显示之后*/
UIKIT_EXTERN NSNotificationName const UIMenuControllerDidShowMenuNotification __TVOS_PROHIBITED;
/*菜单隐藏之前*/
UIKIT_EXTERN NSNotificationName const UIMenuControllerWillHideMenuNotification __TVOS_PROHIBITED;
/*菜单隐藏之后*/
UIKIT_EXTERN NSNotificationName const UIMenuControllerDidHideMenuNotification __TVOS_PROHIBITED;
/*菜单框架改变时*/
UIKIT_EXTERN NSNotificationName const UIMenuControllerMenuFrameDidChangeNotification __TVOS_PROHIBITED;

UIMenuItem对象:

/*创建并返回具有给定标题和操作初始化的菜单项对象*/
- (instancetype)initWithTitle:(NSString *)title action:(SEL)action NS_DESIGNATED_INITIALIZER;
/*菜单项的标题*/
@property(nonatomic,copy) NSString *title;
/*选择器,用于识别响应于菜单命令的响应对象的调用方法*/
@property(nonatomic)      SEL       action;

案例:

.h文件里声明

@property (nonatomic, strong) UILabel  *contentLabel;

.m文件实现

/*创建手势视图*/
self. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 20)];
self. contentLabel.font = [UIFont systemFontOfSize:16.0f];
self. contentLabel.backgroundColor=[UIColor grayColor];
self. contentLabel.textColor=[UIColor whiteColor];
self. contentLabel.text=@"hello china";
[self.view addSubview: self. contentLabel];
 /*开启用户交互的总开关*/
contentLabel.userInteractionEnabled = YES; 
/*添加长按手势识别*/
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenuView)];
 [self. contentLabel addGestureRecognizer:longGesture];

创建菜单选项框

- (void)showMenuView
{
    /*请求成为第一响应者*/
    [self. contentLabel becomeFirstResponder];
   /*初始化菜单项*/
    UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copy:)];
    UIMenuItem *delectItem = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(delect:)];
    /*获取菜单控制器*/
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:copyItem, delectItem, nil]];
    /*设置菜单显示的位置信息*/
    [menu setTargetRect:CGRectMake(self. contentLabel.frame.origin.x, self. contentLabel.frame.origin.y, self. contentLabel.frame.size.width, 30) inView:self. contentLabel];
    /*显示菜单*/
    [menu setMenuVisible:YES animated:YES];
}
#pragma 为了能接收到事件(能成为第一响应者),我们需要覆盖一个方法,以及针对复制的操作覆盖两个方法
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
#pragma 请求接收响应器启用或禁用用户界面中的指定命令
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    [super canPerformAction:action withSender:sender];
    if ( action == @selector(copy:) || action == @selector(delect:))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

针对于响应方法的实现

#pragma mark 长按复制操作
- (void)copy:(id)sender
{
    DDLogInfo(@"复制消息内容...");
}
#pragma mark 长按删除操作
- (void)delect:(id)sender
{
   DDLogInfo(@"删除消息内容...");
}

你可能感兴趣的:(UIMenuController类详解)