UIMenuController使用 /模擬朋友圈收藏/復(fù)制彈窗

image.png
關(guān)于UIMenuController

UIMenuController單例是由系統(tǒng)管理觸碰隱藏邏輯的控件,特別在UITableView中cell需要這樣的彈窗效果,顯示隱藏等邏輯操作,為開(kāi)發(fā)者省去很多時(shí)間。

使用直接上代碼:
//
//  ViewController.m
//  UIMenuControllerDemo
//
//  Created by Grabin on 2017/11/25.
//  Copyright ? 2017年 GrabinWong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *abtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

- (void)setUpUI
{
    _abtn = [[UIButton alloc] initWithFrame:CGRectMake(55.0, 80.0, 220.0, 40.0)];
    [_abtn setTitle:@"這是個(gè)按鈕" forState:UIControlStateNormal];
    [_abtn setBackgroundColor:[UIColor orangeColor]];
    [_abtn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_abtn];
}

- (void)clickBtnAction:(UIButton *)abtn
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController.isMenuVisible) {
        [menuController setMenuVisible:NO animated:YES];
    } else {
        [self becomeFirstResponder]; //此代碼比較重要,當(dāng)自定義view的時(shí)候,會(huì)導(dǎo)致item出不來(lái)
        UIMenuItem *item0 = [[UIMenuItem alloc]initWithTitle:@"復(fù)制" action:@selector(copyAction)];
        UIMenuItem *item1 = [[UIMenuItem alloc]initWithTitle:@"刪除" action:@selector(deletedAction)];
        menuController.menuItems = @[item0,item1];
        menuController.arrowDirection = UIMenuControllerArrowUp;
        [menuController setTargetRect:abtn.frame inView:self.view];
        [menuController setMenuVisible:YES animated:YES];
    }
}

// 是否能成為第一響應(yīng)者
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

// 用于UIMenuController顯示,缺一不可
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if (action == @selector(copyAction)
        || action ==@selector(deletedAction)){
        return YES;
    }
    return NO;//隱藏系統(tǒng)默認(rèn)的菜單項(xiàng)
}

- (void)copyAction
{
    NSLog(@"%s 第%d行",__FUNCTION__,__LINE__);
}

- (void)deletedAction
{
    NSLog(@"%s 第%d行",__FUNCTION__,__LINE__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end

注意的點(diǎn)在于

一、是否能成為第一響應(yīng)者
// 是否能成為第一響應(yīng)者
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
二、隱藏系統(tǒng)默認(rèn)的菜單項(xiàng) & 顯示自定義菜單
// 用于UIMenuController顯示,缺一不可
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if (action == @selector(copyAction)
        || action ==@selector(deletedAction)){
        return YES;
    }
    return NO;//隱藏系統(tǒng)默認(rèn)的菜單項(xiàng)
}
三、成為第一響應(yīng)者
image.png
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 今天看了一會(huì)兒《假如給我三天光明》這本書(shū),突然覺(jué)得文字是個(gè)很神奇而且奇妙的東西,作者讓不同的文字組織在一起,就可以...
    不文藝的田螺姑娘閱讀 648評(píng)論 0 1
  • 皇后秘制美食做了有一段時(shí)間了,從剛開(kāi)始的摸索,到現(xiàn)在小有經(jīng)驗(yàn),這讓我感到覺(jué)很有收獲。任何事情都是在不斷的摸索中前進(jìn)...
    xyz心若幽蘭閱讀 199評(píng)論 2 2
  • 讀完李砍柴的文章《我沉默內(nèi)向敏感情緒化,但我是個(gè)好孩子》,對(duì)他所描述的那種一個(gè)沉默內(nèi)向之人安靜的外表下心中存在的“...
    任西東閱讀 2,592評(píng)論 32 38
  • 獨(dú)憐幽草澗邊生 上有黃鸝深樹(shù)鳴 春潮帶雨晚來(lái)急 野渡無(wú)人舟自橫 喜歡的朋友,麻煩你點(diǎn)個(gè)贊唄!謝謝啦!
    萍手繪閱讀 898評(píng)論 3 10

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