在自定义视图/ uiview子类上实现iphone的复制/粘贴控件 [英] Implementing iphone's copy/paste controls on a custom view / uiview subclass

查看:171
本文介绍了在自定义视图/ uiview子类上实现iphone的复制/粘贴控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I will admit that there is already a question exactly along these lines here on S.O., but it lacks implementation details, a working answer, and I would like to be more specific, so I think a new question is in order. Obviously, let me know if I'm wrong and we can try to restart the thread over there.

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

Basically, I want to copy the text in a UILabel to the pasteboard when a user holds down on the label. Not hard to do, honestly. However, I think the best way to provide visual feedback is to prompt the user with the Copy menu option from UIMenuController.

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

According to the Event Handling section of the iPhone Application Programming Guide, specifically the section on Copy, Cut, and Paste Operations, it should be possible to provide copy, cut, and/or paste operations from a custom view.

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

So, I've sub-classed UILabel with the following implementation as described by the guide, but the UIMenuController won't show up. There's no indication in the guide that there's anything else required to do this, and the NSLog statement does print out to the console, indicating that the selector is being performed when I hold down on the label:

//
//  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

那么,还有什么呢?必须做到这一点?

So, what else does one have to do to make this happen?

对于那些追随并尝试这样做的人,您还需要为标签设置用户互动已启用

编辑:

为清楚起见,我要补充一点,这应该与小[Copy]菜单项相似当你按住它时,它会出现在某些iphone视图中的图像上。 -B

For clarity, let me add that this should be similar to the little [Copy] menu item that appears over an image in certain iphone views when you hold down on it. -B

推荐答案

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

I'll say upfront I don't have an aswer, but I did some poking around and found more out. I'm sure you've looked at this already: CopyPasteTile

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

That code does work on my simulator and goes like this:

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获得)

  • drawRect is calculated from a giant view tile and tap point calculations
  • setNeedsDisplayInRect is being called
  • self is a large screen sized view, you may need screen coords instead of local coords (you can probably get that from self.superview)

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

I'd try making these adjustments to match the example first and see what kind of progress it gets me.

这篇关于在自定义视图/ uiview子类上实现iphone的复制/粘贴控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆