在应用程序中也可以使用剪切板时需要使用UIPasteboard类,使用的第一个步骤是取得UIPasteBoard的单一实例(single instance),可以通过generalPasteboard方法取得此种单一实例。
编辑菜单可以通过UIMenuController类在应用程序中使用此编辑菜单。UIMenuController 通过sharedMenuController属性取得单一实例,例如需要两次触摸画面时显示copy等,
首先,因为编辑菜单只能在第一响应者中显示,如果默认非第一响应者的情况下,需要重写UIResponder 的canBecomeFirstresponder方法并返回 YES.
接着,需要重写UIResponder的canPerformAction:withSender:方法,让需要显示菜单返回YES。
新建一个单视图控制器工程。
下面我们就结合实际的代码来说明吧
具体的代码如下:
HHLAppDelegate.h
#import <UIKit/UIKit.h>
@class HHLViewController;
@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHLViewController *viewController;
@end
HHLAppDelegate.m
#import "HHLAppDelegate.h"
#import "HHLViewController.h"
@implementation HHLAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
HHLViewController.h
#import <UIKit/UIKit.h>
@interface HHLViewController : UIViewController
{
@private
CGPoint touchPoint;
}
@end
HHLViewController.m
#import "HHLViewController.h"
@interface HHLViewController ()
- (UIImageView *)imageContainsPoint:(CGPoint)point;
@end
@implementation HHLViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor whiteColor];
UIImage *image = [UIImage imageNamed:@"bug1.png"];
UIImageView *bug1 = [[UIImageView alloc]initWithImage:image];
bug1.center = self.view.center;
[self.view addSubview:bug1];
[bug1 release];
// image = [UIImage imageNamed:@"bug2.png"];
// UIImageView *bug2 = [[UIImageView alloc]initWithImage:image];
//
// bug2.center = self.view.center;
// [self.view addSubview:bug2];
// [bug2 release];
//
//
// image = [UIImage imageNamed:@"bug3.png"];
//
// UIImageView *bug3 = [[UIImageView alloc]initWithImage:image];
// bug3.center = self.view.center;
// [self.view addSubview:bug3];
// [bug3 release];
}
//不成为第一响应这无法显示编辑菜单
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([self becomeFirstResponder] && 1 < [touch tapCount]) {
//连续两次碰触后显示编辑菜单
UIMenuController *menu = [UIMenuController sharedMenuController];
touchPoint = [touch locationInView:self.view];
CGRect minRect;
minRect.origin = touchPoint;
[menu setTargetRect:minRect inView:self.view];
[menu setMenuVisible:YES animated:YES];
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (@selector(copy:) == action) {
if ([self imageContainsPoint:touchPoint]) {
return YES;
}
}else if (@selector(cut:) == action){
if ([self imageContainsPoint:touchPoint]) {
return YES;
}
}else if (@selector(paste:) == action){
return (nil != [UIPasteboard generalPasteboard].image);
}
return NO;
}
- (UIImageView *)imageContainsPoint:(CGPoint)point
{
for (UIView *view in self.view.subviews) {
if (CGRectContainsPoint(view.frame, point)) { //如果point 被包含在view.frame内则返回true,否则返回false
if ([view isKindOfClass:[UIImageView class]]) {
return (UIImageView *)view;
}
}
}
return nil;
}
- (void)copy:(id)sender
{
UIImageView *imageView = [self imageContainsPoint:touchPoint];
if (imageView) {
[UIPasteboard generalPasteboard].image = imageView.image;
}
}
- (void)cut:(id)sender
{
UIImageView *imageView = [self imageContainsPoint:touchPoint];
if (imageView) {
[UIPasteboard generalPasteboard].image = imageView.image;
[imageView removeFromSuperview];
}
}
- (void)paste:(id)sender
{
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
if (pasteBoard) {
UIImageView *bug = [[[UIImageView alloc]initWithImage:pasteBoard.image]autorelease];
bug.center = touchPoint;
[self.view addSubview:bug];
pasteBoard.image = bug.image;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行程序后,效果如下:
触摸两次图片后的情况如下:
选择copy,进行操作 在空白处触摸两次,然后效果图如下:
鼠标放上去选择paste,后效果如图所示:
其实很简单,是不是呀,只要记下那几个函数,就可以了,以后用到的时候套用即可。