在自定义视图/ uiview子类上实现iphone的复制/粘贴控件
我会承认,这里已经有一个关于SO的问题,但它缺少实现细节,工作答案,我想要更多具体,所以我认为一个新的问题是为了。显然,让我知道如果我错了,我们可以尝试restart the thread over there。在自定义视图/ uiview子类上实现iphone的复制/粘贴控件
基本上,我想在用户按下标签时将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
我会说前面我没有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];
这里有一些区别:
- 的drawRect是从一个巨大的视图瓦片和抽头计算得出的
-
setNeedsDisplayInRect
被称为 -
self
是一个大屏幕尺寸的视图,你可能需要屏幕COORDS而不是本地COORDS(你也许可以得到从self.superview)
我会尝试进行这些调整先匹配的例子,看看什么样的进步它得到我。
aaahhh,有趣!会给它一个镜头,谢谢;-) – 2010-01-21 17:06:35
宾果,尝试了你的每一个建议,并赢家设置'selectionRect = [[超级视图]帧]'。将写出一个完整的工作示例,并很快将其链接到此处。谢谢! – 2010-01-21 17:46:17
很高兴听到你把它修好:) – slf 2010-01-21 18:36:34
一旦我得到了这个工作,我将一个教程和示例项目放在Mobile Orchard上:http://www.mobileorchard.com/hold-and-copy-in-uikit/ – 2010-02-25 15:37:21
教程正是我需要的,但你忘了添加“重置”方法代码。谢谢! – AlBeebe 2011-07-31 06:16:46