在自定义视图/ uiview子类上实现iphone的复制/粘贴控件

Bil*_*ray 7 iphone copy-paste uimenucontroller

我承认在SO上已经存在这些问题,但它缺乏实现细节,工作答案,我想更具体,所以我认为一个新问题是有道理的.显然,让我知道如果我错了,我们可以尝试在那里重新启动线程.

基本上,我想在用户按住标签时将UILabel中的文本复制到粘贴板.老实说,不难做到.但是,我认为提供视觉反馈的最佳方法是使用"复制"菜单选项提示用户UIMenuController.

根据"iPhone应用程序编程指南"的"事件处理"部分,特别是" 复制","剪切"和"粘贴操作"部分,应该可以从自定义视图中提供复制,剪切和/或粘贴操作.

因此,我已经按照指南所描述的以下实现对UILabel进行了分类,但UIMenuController将不会显示.指南中没有任何迹象表明还有其他任何操作要求,并且NSLog语句会打印到控制台,指示当我按住标签时正在执行选择器:

//
//  CopyLabel.m
//  HoldEm
//
//  Created by Billy Gray on 1/20/10.
//  Copyright 2010 Zetetic LLC. All rights reserved.
//

#import "CopyLabel.h"

@implementation CopyLabel

- (void)showCopyMenu {
    NSLog(@"I'm tryin' Ringo, I'm tryin' reeeeal hard.");
    // bring up editing menu.
    UIMenuController *theMenu = [UIMenuController sharedMenuController];
    // do i even need to show a selection? There's really no point for my implementation...
    // doing it any way to see if it helps the "not showing up" problem...
    CGRect selectionRect = [self frame];
    [theMenu setTargetRect:selectionRect inView:self];
    [theMenu setMenuVisible:YES animated:YES]; // <-- doesn't show up...
}

// obviously, important to provide this, but whether it's here or not doesn't seem
// to change the fact that the UIMenuController view is not showing up
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    BOOL answer = NO;

    if (action == @selector(copy:))
        answer = YES;

    return answer;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(showCopyMenu) withObject:nil afterDelay:0.8f];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCopyMenu) object:nil];
}

@end
Run Code Online (Sandbox Code Playgroud)

那么,为了实现这一目标还需要做些什么呢?

对于那些跟随并尝试这样做的人,您还需要为标签设置"User Interaction Enabled"

编辑:

为清楚起见,我要补充一点,这应该类似于按住它时某些iphone视图中图像上显示的小[复制]菜单项.-B

slf*_*slf 7

我会先说我没有aswer,但我做了一些探索并找到了更多.我相信你已经看过这个了:CopyPasteTile

该代码在我的模拟器上工作,如下所示:

CGRect drawRect = [self rectFromOrigin:currentSelection inset:TILE_INSET];
[self setNeedsDisplayInRect:drawRect];

UIMenuController *theMenu = [UIMenuController sharedMenuController];
[theMenu setTargetRect:drawRect inView:self];
[theMenu setMenuVisible:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

这里有一些不同之处:

  • drawRect是从巨型视图图块和分接点计算中计算出来的
  • setNeedsDisplayInRect 被称为
  • self 是一个大屏幕大小的视图,你可能需要屏幕坐标而不是本地坐标(你可以从self.superview获得)

我会先尝试进行这些调整以匹配示例,看看它能带来什么样的进展.

  • 比利 - 完整的例子在哪里?:-) (2认同)