标签:
#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"); }
标签:
原文地址:http://www.cnblogs.com/jayzhang/p/4688762.html