zoukankan      html  css  js  c++  java
  • 对常用控件添加长按复制及自定义弹出menu

    #import "FeedbackLabel.h"
    
    @implementation FeedbackLabel
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    - (BOOL)canBecomeFirstResponder{
    return YES;
    }
    
    //"反馈"关心的功能
    -(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;
        }
            return NO;
    }
    //针对于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(handTapLabel:)];
        touch.numberOfTapsRequired = 2;
        [self addGestureRecognizer:touch];
        //长按压
        UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        press.minimumPressDuration = 1.0;
        [self addGestureRecognizer:press];
        
    }
    //绑定事件
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self attachTapHandler];
        }
        return self;
    }
    
    -(void)handTapLabel:(UIGestureRecognizer*) recognizer{
        [self becomeFirstResponder];
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setTargetRect:self.frame inView:self.superview];
        [menu setMenuVisible:YES animated:YES];
    }
    
    - (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];
        }  
    }
    
    - (void)flag:(id)sender {
        NSLog(@"Cell was flagged");
    }
    
    - (void)approve:(id)sender {
        NSLog(@"Cell was approved");
    }
    
    - (void)deny:(id)sender {
        NSLog(@"Cell was denied");
    }
  • 相关阅读:
    Python---Flask--08--Flask-Ckeditor
    Python---Flask--07--SQLAlchemy基本关系
    Python---Flask--06--分页的实现
    Python---Flask--05--g对象和钩子函数
    maven项目管理构建
    POI 设置
    http状态码
    hibernate框架之-查询结果集返回类型
    Struts2框架之-注解开发
    Struts2框架之-Struts2的标签
  • 原文地址:https://www.cnblogs.com/jayzhang/p/4688762.html
Copyright © 2011-2022 走看看