• 对常用控件添加长按复制及自定义弹出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");
    }
  • 相关阅读:
    ndt histogram_direction
    rplidar & hector slam without odometry
    点云的基本几何计算
    rplidar测试
    使用ZXing.Net生成与识别二维码(QR Code)
    C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码
    .NET 二维码生成(ThoughtWorks.QRCode)
    zxing二维码的生成与解码(C#)
    netsh interface portproxy的一个简单例子
    Redis 客户端连接
  • 原文地址:https://www.cnblogs.com/jayzhang/p/4688762.html
Copyright © 2020-2023  润新知