• UIMenuController的使用,对UILabel拷贝以及定制菜单 .


    1. Menu所处的View必须实现 – (BOOL)canBecomeFirstResponder, 且返回YES

    2. Menu所处的View必须实现 – (BOOL)canPerformAction:withSender, 并根据需求返回YES或NO

    3. 使Menu所处的View成为First Responder (becomeFirstResponder)

    4. 定位Menu (- setTargetRect:inView:)

     

    5. 展示Menu (- setMenuVisible:animated:)

    1. @implementation UICopyLabel  
    2.   
    3. // default is NO   
    4. - (BOOL)canBecomeFirstResponder{  
    5.     return YES;  
    6. }  
    7.   
    8. //"反馈"关心的功能     
    9. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{    
    10.     return (action == @selector(copy:));    
    11. }    
    12. //针对于copy的实现     
    13. -(void)copy:(id)sender{    
    14.     UIPasteboard *pboard = [UIPasteboard generalPasteboard];    
    15.     pboard.string = self.text;    
    16. }   
    17.   
    18. //UILabel默认是不接收事件的,我们需要自己添加touch事件     
    19. -(void)attachTapHandler{    
    20.     self.userInteractionEnabled = YES;  //用户交互的总开关     
    21.     UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];    
    22.     touch.numberOfTapsRequired = 2;    
    23.     [self addGestureRecognizer:touch];    
    24.     [touch release];    
    25. }    
    26. //绑定事件     
    27. - (id)initWithFrame:(CGRect)frame    
    28. {    
    29.     self = [super initWithFrame:frame];    
    30.     if (self) {    
    31.         [self attachTapHandler];    
    32.     }    
    33.     return self;    
    34. }    
    35. //同上     
    36. -(void)awakeFromNib{    
    37.     [super awakeFromNib];    
    38.     [self attachTapHandler];    
    39. }  
    40.   
    41. -(void)handleTap:(UIGestureRecognizer*) recognizer{    
    42.     [self becomeFirstResponder];    
    43.     UIMenuController *menu = [UIMenuController sharedMenuController];    
    44.     [menu setTargetRect:self.frame inView:self.superview];    
    45.     [menu setMenuVisible:YES animated:YES];    
    46. }   
    47.   
    48.   
    49. @end  
    @implementation UICopyLabel
    
    // default is NO
    - (BOOL)canBecomeFirstResponder{
        return YES;
    }
    
    //"反馈"关心的功能  
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
        return (action == @selector(copy:));  
    }  
    //针对于copy的实现  
    -(void)copy:(id)sender{  
        UIPasteboard *pboard = [UIPasteboard generalPasteboard];  
        pboard.string = self.text;  
    } 
    
    //UILabel默认是不接收事件的,我们需要自己添加touch事件  
    -(void)attachTapHandler{  
        self.userInteractionEnabled = YES;  //用户交互的总开关  
        UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];  
        touch.numberOfTapsRequired = 2;  
        [self addGestureRecognizer:touch];  
        [touch release];  
    }  
    //绑定事件  
    - (id)initWithFrame:(CGRect)frame  
    {  
        self = [super initWithFrame:frame];  
        if (self) {  
            [self attachTapHandler];  
        }  
        return self;  
    }  
    //同上  
    -(void)awakeFromNib{  
        [super awakeFromNib];  
        [self attachTapHandler];  
    }
    
    -(void)handleTap:(UIGestureRecognizer*) recognizer{  
        [self becomeFirstResponder];  
        UIMenuController *menu = [UIMenuController sharedMenuController];  
        [menu setTargetRect:self.frame inView:self.superview];  
        [menu setMenuVisible:YES animated:YES];  
    } 
    
    
    @end

    在view里添加一个UICopyLabel

    现在可以使用UICopyLabel实现双击来对label的内容copy了

    在你的view中

    UICopyLabel *display = [[UICopyLabelalloc]initWithFrame:CGRectMake(30,100,250,30)];

     

     

    awakeFromNib

    在使用IB的时候才会涉及到此方法的使用,当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。

    看例子:

    创建一个viewController with XIB

     

     

    定义一个UIView的子类

     

    打开xib,并把View的类型指定为上一步骤定义的子类

     

    然后在TestView.m中加入 awakeFromNib方法,运行程序发现此方法被调用了!!!

     

     

    下面我们来定制菜单

    attachTapHandler中添加长按压手势

     

     

    1. -(void)attachTapHandler{    
    2.     self.userInteractionEnabled = YES;  //用户交互的总开关     
    3.     //双击   
    4.     UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];    
    5.     touch.numberOfTapsRequired = 2;    
    6.     [self addGestureRecognizer:touch];   
    7.      [touch release];   
    8.       
    9.     //长按压   
    10.     UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];  
    11.     press.minimumPressDuration = 1.0;  
    12.     [self addGestureRecognizer:press];  
    13.     [press release];  
    14.       
    15. }   
    -(void)attachTapHandler{  
        self.userInteractionEnabled = YES;  //用户交互的总开关  
        //双击
        UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];  
        touch.numberOfTapsRequired = 2;  
        [self addGestureRecognizer:touch]; 
         [touch release]; 
        
        //长按压
        UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        press.minimumPressDuration = 1.0;
        [self addGestureRecognizer:press];
        [press release];
        
    } 

    添加方法longPress

     

    1. - (void)longPress:(UILongPressGestureRecognizer *)recognizer {  
    2. if (recognizer.state == UIGestureRecognizerStateBegan) {  
    3. //   TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;   
    4.         [self becomeFirstResponder];  
    5.         UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];  
    6.         UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];  
    7. UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];  
    8.           
    9.         UIMenuController *menu = [UIMenuController sharedMenuController];  
    10. [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];  
    11. [menu setTargetRect:self.frame inView:self.superview];  
    12.         [menu setMenuVisible:YES animated:YES];  
    13.          NSLog(@"menuItems:%@",menu.menuItems);  
    14. }  
    15. }  
    16.   
    17. - (void)flag:(id)sender {  
    18. NSLog(@"Cell was flagged");  
    19. }  
    20.   
    21. - (void)approve:(id)sender {  
    22. NSLog(@"Cell was approved");  
    23. }  
    24.   
    25. - (void)deny:(id)sender {  
    26. NSLog(@"Cell was denied");  
    27. }  
    - (void)longPress:(UILongPressGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
    //	 TSTableViewCell *cell = (TSTableViewCell *)recognizer.view;
            [self becomeFirstResponder];
            UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag" action:@selector(flag:)];
            UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve" action:@selector(approve:)];
    UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny" action:@selector(deny:)];
            
            UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];
    [menu setTargetRect:self.frame inView:self.superview];
            [menu setMenuVisible:YES animated:YES];
             NSLog(@"menuItems:%@",menu.menuItems);
    }
    }
    
    - (void)flag:(id)sender {
    NSLog(@"Cell was flagged");
    }
    
    - (void)approve:(id)sender {
    NSLog(@"Cell was approved");
    }
    
    - (void)deny:(id)sender {
    NSLog(@"Cell was denied");
    }
    

    修改canPerformAction

     

    1. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{    
    2. //    return (action == @selector(copy:));     
    3.     if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {  
    4.         return YES;  
    5.     }  
    6. }  
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
    //    return (action == @selector(copy:));  
        if (action == @selector(copy:)||action == @selector(flag:)||action == @selector(approve:)||action == @selector(deny:)) {
            return YES;
        }
    }

     

    ok。。。效果如图

  • 相关阅读:
    【NLP-09】textCNN
    【NLP-08】textRNN
    【NLP-07】GloVe(Global Vectors for Word Representation)
    【NLP-06】fastText文本分类算法
    【NLP-05】Doc2vec
    mongo用户认证
    find直接copy大于某一个时间小于某一个时间的文件
    es的settings设置详解
    py笔记第一篇
    Linux inode节点使用率过大处理办法
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2966249.html
Copyright © 2020-2023  润新知