美文网首页
UIMenuController UIPasteBoard

UIMenuController UIPasteBoard

作者: 227f600b867c | 来源:发表于2016-06-27 14:58 被阅读39次
    
    - (void)setupUI {
        self.tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        self.tapView.center = self.view.center;
        self.tapView.backgroundColor = [UIColor redColor];
    
        UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap)];
        [self.tapView addGestureRecognizer:recognizer];
    
        [self.view addSubview:self.tapView];
    }
    
    - (void)longTap {
        [self becomeFirstResponder];
        UIMenuController *menuControler = [UIMenuController sharedMenuController];
        UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"ccccccccccc" action:@selector(copyToPasteBoard)];
        [menuControler setMenuItems:[NSArray arrayWithObjects:item, nil]];
    
        menuControler.arrowDirection = UIMenuControllerArrowUp;
        [menuControler setMenuVisible:NO];
        [menuControler setTargetRect:self.tapView.frame inView:self.view];
        [menuControler setMenuVisible:YES animated:YES];
    }
    
    - (void)copyToPasteBoard {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = @"Test String";
    }
    
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if (action != @selector(copyToPasteBoard)) {
            return NO;
        }
        return YES;
    }
    
    

    相关文章

      网友评论

          本文标题:UIMenuController UIPasteBoard

          本文链接:https://www.haomeiwen.com/subject/fgvzdttx.html