• 自定义粘贴板-陈鹏


    接下来我们做一个可复制的UIImageView,创建一个新的viewController,放两个imageView,默认显示不同的图:

    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
        return (action == @selector(copy:) || action == @selector(paste:));  
    } //标识可运行的方法
    
    -(void)copy:(id)sender{  
        UIPasteboard *pboard = [UIPasteboard generalPasteboard];  
        pboard.image = self.image;  
    } //拷贝的方法
    
    -(void)paste:(id)sender{  
        UIPasteboard *pboard = [UIPasteboard generalPasteboard];  
        self.image = pboard.image;  
    }//粘贴的方法
    //自定义粘贴显示菜单

    1. 为了方便实用,我给cell添加了长按事件,看代码---

      UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

          [cell addGestureRecognizer:recognizer];

      好,事件加上了,剩下的点击就是实现自定义剪贴板了,上代码:

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

             if (recognizer.state == UIGestureRecognizerStateBegan) {

                   CopyCell *cell = (CopyCell *)recognizer.view;

                   [cell 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]];

              NSLog(@".....%@",NSStringFromCGRect(cell.frame));

              [menu setTargetRect:cell.frame inView:cell.superview];


              [menu setMenuVisible:YES animated:YES];

            }

      }

      通过上面这段代码,自定义剪贴板成功搞定,运行,你猛然会发现,操,怎么剪贴板没有出来,我猜你已经想到为什么了,因为

      - (BOOL)canBecomeFirstResponder{

          return YES;

      }

      这么重要的一句话没有加,OK,下面就是你所要使用的方法了

      - (void)flag:(id)sender {


          NSLog(@"Cell was flagged");


      }

      - (void)approve:(id)sender {


          NSLog(@"Cell was approved");

      }


      - (void)deny:(id)sender {


          NSLog(@"Cell was denied");


      }

  • 相关阅读:
    工厂模式一
    面向对象的简单理解二
    工厂模式三
    线程的简单学习
    nyoj35 表达式求值
    nyoj305 表达式求值
    poj1298 The Hardest Problem Ever
    poj1363 Rails
    hdu2036 改革春风吹满地
    nyoj467 中缀式变后缀式
  • 原文地址:https://www.cnblogs.com/sixindev/p/4588049.html
Copyright © 2020-2023  润新知