編輯菜單的顯示和管理

簡(jiǎn)介

Displaying and Managing the Edit Menu 官方文檔

管理選擇和編輯菜單

  • 觸發(fā)顯示編輯菜單,注意需要能成為第一響應(yīng)者才行,看情況是否需要繼承處理。
- (void)tapHandler {
    if (![self becomeFirstResponder]) {
        NSLog(@"error");
        return;
    }
    
    UIMenuController *menu=[UIMenuController sharedMenuController];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES];
}
  • 選擇性顯示
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(cut:)) {
        return YES;
    }
    return NO;
}

添加自定義項(xiàng)目

  • 一般是UIResponder類的子類即可,比如繼承UIImage,注意需要使用點(diǎn)擊或者其他方式觸發(fā)
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {        
        self.userInteractionEnabled = YES;
       
        UITapGestureRecognizer *pressGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandler)];
        [self addGestureRecognizer:pressGesture];
        
    }
    return self;
}
  • 必須第一響應(yīng)者才會(huì)觸發(fā)編輯菜單
- (BOOL)canBecomeFirstResponder {
    return YES;
}
  • 觸發(fā)顯示編輯菜單
- (void)tapHandler {
    if (![self becomeFirstResponder]) {
        NSLog(@"error");
        return;
    }
    
    UIMenuController *menu=[UIMenuController sharedMenuController];
    
    // 自定義的菜單,注意seletor需要選擇在后面顯示
    UIMenuItem *pop = [[UIMenuItem alloc]initWithTitle:@"Pop" action:@selector(popSelf)];
    
    menu.menuItems=@[pop];
    [menu setTargetRect:self.bounds inView:self];
    menu.arrowDirection=UIMenuControllerArrowDown;
    [menu update];
    [menu setMenuVisible:YES];
}
  • 選擇菜單按鈕顯示
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(popSelf)) {?
        return YES;
    }
    return NO;
}
  • 自定義的菜單方法
- (void)popSelf {
    NSLog(@"pop");
}

隱藏編輯菜單

  • 默認(rèn)自動(dòng)隱藏。
  • 關(guān)于編輯菜單,還有額外的通知可供使用。

我整理的文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容