我将承认,在S.O.上已经完全按照这些思路提出了一个问题,但是它缺少实现细节,有效的答案,并且我想更具体一些,所以我认为有一个新的问题正在提出。显然,让我知道我是否错了,我们可以尝试restart the thread over there。
基本上,我想在用户按住标签时将UILabel中的文本复制到粘贴板。老实说,这并不难。但是,我认为提供视觉反馈的最佳方法是使用UIMenuController
中的“复制”菜单选项来提示用户。
根据《 iPhone应用程序编程指南》的“事件处理”部分,尤其是Copy, Cut, and Paste Operations部分,应该可以从自定义视图提供复制,剪切和/或粘贴操作。
因此,按照指南中的描述,我将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
那么,要实现这一目标还需要做什么?
对于那些后续并尝试执行此操作的用户,您还需要为标签设置“已启用用户互动”
编辑:
为了清楚起见,让我补充说,这应该类似于在按住某些iphone视图时在图像上方显示的小[复制]菜单项。 -B
最佳答案
我会先说我没有要求,但是我四处摸索发现了更多信息。我确定您已经看过了: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];
这里有一些区别:
drawRect是从巨型视图图块和点击点计算得出的
正在呼叫
setNeedsDisplayInRect
self
是大屏幕尺寸的视图,您可能需要屏幕坐标而不是局部坐标(您可能可以从self.superview中获得)我会尝试进行这些调整以首先匹配示例,然后看看它能给我带来什么样的进步。