zoukankan      html  css  js  c++  java
  • iOS中UIMenuController的使用

    不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意:

    这里写图片描述

    要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResponder方法,其次要实现canBecomeFirstResponder方法,并返回YES.

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @property(nonatomic,strong)UILabel *label;

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        _label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 200, 50)];

        _label.text = @"我是一个label";

        _label.textAlignment = NSTextAlignmentCenter;

        _label.textColor = [UIColor blackColor];

        [self.view addSubview:_label];

        

        self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

        //

        _label.userInteractionEnabled = YES;

        //添加长按手势

        [_label addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]];

    }

     

    -(void)longPress:(UILongPressGestureRecognizer *)sender{

     

        if (sender.state == UIGestureRecognizerStateBegan) {

            [self.view becomeFirstResponder];

            _label.backgroundColor = [UIColor lightGrayColor];

            UIMenuController *menu = [UIMenuController sharedMenuController];

            //复制

            UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyItemClicked:)];

            

            //收藏

            UIMenuItem *collectItem = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(collectItemClicked:)];

            

            //举报

            UIMenuItem *reportItem = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(reportItemClicked:)];

            

            menu.menuItems = @[copyItem,collectItem,reportItem];

            

            [menu setMenuVisible:YES animated:YES];

            

            [menu setTargetRect:_label.frame inView:self.view];

        }

        if (sender.state==UIGestureRecognizerStateEnded) {

            _label.backgroundColor = [UIColor clearColor];

        }

    }

     

    - (void)copyItemClicked:(UIMenuItem *)item

    {

        NSLog(@"复制");

    }

     

    - (void)collectItemClicked:(UIMenuItem *)item

    {

        NSLog(@"收藏");

    }

     

    - (void)reportItemClicked:(UIMenuItem *)item

    {

        NSLog(@"举报");

    }

     

    - (BOOL)canBecomeFirstResponder

    {

        return YES;

    }

     

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

  • 相关阅读:
    python里面的xlrd模块详解以及样例
    关于DOM的事件操作
    python正则表达式去除文本中间的换行符
    文本分类问题汇总
    pip安装问题
    3NF的无损连接和保持函数依赖的分解、BCNF的无损连接的分解
    Pyhton基本图形绘制
    软件过程模型
    常见算法的时间与空间复杂度
    随笔
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/4982232.html
Copyright © 2011-2022 走看看