app等级的只能在app内进行复制和粘贴,必威一、

@implementation ViewController- viewDidLoad { [super viewDidLoad]; CopyView* cv = [[CopyView alloc] initWithFrame:self.view.bounds]; self.view = cv;}@end
  • (void)removePasteboardWithName:(NSString )pasteboardName;
    /
    从粘贴板中获取数据,pasteboardType是自定义的,表明app能够拍卖哪一类档案的次序的多寡*/

@implementation CopyView

1.概述

UIPasteboard是ios中做客粘贴板的原生控件,可分为系统品级的和app品级的,系统等级的独立于app,能够复制七个app的始末到另二个app;app等级的只好在app内实行复制和粘贴;它们分别由+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create那七个类方式进行创办。

地点3个伊始化方法,分获或创办3个等第不一致的剪切板,上面大家详解一下在怎么着动静下用哪一类初步化方法

在ios中,支持UIPasteboard原生控件唯有UITextField、UITextView、UIWebView那七个,若是想自定义三个控件能够利用UIPasteboard,要求在概念的时候重载 -canBecomeFirstResponder-canPerformAction:action withSender:sender那多少个点子,前者设置控件可称为第一响应器,前者决定以此控件能够接纳复制、剪切、选中、全选、粘贴等的哪类或两种功用,一碗水端平载对应的-copy:sender-cut:sender-select:sender-selectAll:sender-paste:sender方法,在那些方法中处理事件,UIMenuController担任呈现UI。

import "ViewController.h"

//获取一组数据对象中是或不是带有有些数据类型

3.行使方法

在ios中,协助UIPasteboard原生控件只有UITextField、UITextView、UIWebView那多少个,假若想自定义二个控件可以选择UIPasteboard,需求在概念的时候重载 -(BOOL)canBecomeFirstResponder和 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender那五个方法,后面一个设置控件可称之为第一响应器,前者决定以此控件能够使用复制、剪切、选中、全选、粘贴等的哪种或三种效率,仁同一视载对应的-(void)copy:(id)sender、-(void)cut:(id)sender、-(void)select:(id)sender、-(void)selectAll:(id)sender、-(void)paste:(id)sender方法,在那些格局中处总管件,UIMenuController担当呈现UI。

对照于地点两组方法,下边那几个点子尤其面向对象,在开辟中行使越来越有益于与便捷:

功能显示

1.概述
UIPasteboard是ios中访谈粘贴板的原生控件,可分为系统等第的和app品级的,系统等第的独立于app,能够复制一个app的内容到另三个app;app等第的只好在app内开展复制和粘贴;它们分别由+ (nullable UIPasteboard )pasteboardWithName:(NSString )pasteboardName create:(BOOL)create那四个类方式实行创办。
2.数据类型
能够复制在粘贴板的数据类型有NSString、UIImage、NSU中华VL、UIColor、NSData以及由这一个种类成分结合的数组。可个别由它们的set方法将数据放在粘贴板中,如NSString:
UIPasteboard pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:@"复制的字符串内容"];
3.认知常用方法
/
因此名称获取粘贴板并且移除*/

//此剪切板的改观次数 系统等级的剪切板唯有当设备再度起动时 这些值才会清零

4.复制图片的简要例子

始建三个CopyView

#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView* img1;
@property (strong, nonatomic) UIImageView* img2;
@end

@implementation CopyView
-(UIImageView *)img1{
    if (_img1 == nil) {
        _img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
        NSString* path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"];
        _img1.image = [UIImage imageWithContentsOfFile:path];
    }
    return _img1;
}

-(UIImageView *)img2{
    if (_img2 == nil) {
          _img2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.img1.frame)+50.0f, 20.0f, 100.0f, 100.0f)];
        _img2.backgroundColor = [UIColor lightGrayColor];
    }
    return _img2;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.img1];
        [self addSubview:self.img2];
    }
    return self;
}

-(BOOL)canBecomeFirstResponder{
    return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"];
    if ([methodNameArr containsObject:NSStringFromSelector(action)]) {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}

-(void)copy:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setImage:self.img1.image];
}

-(void)paste:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    self.img2.image = [pasteboard image];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self becomeFirstResponder];
    UIMenuController* menuController = [UIMenuController sharedMenuController];
    [menuController setTargetRect:self.img1.frame inView:self];
    [menuController setMenuVisible:YES animated:YES];
}

@end

在controller中

#import "ViewController.h"
#import "CopyView.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CopyView* cv = [[CopyView alloc] initWithFrame:self.view.bounds];
    self.view = cv;
}

@end

}

在controller中#import "ViewController.h"#import "CopyView.h"

二、系统的剪切板管理类UIPasteboard

2.数据类型

能够复制在粘贴板的数据类型有NSString、UIImage、NSUENVISIONL、UIColor、NSData以及由这几个品种成分结合的数组。可各自由它们的set方法将数据放在粘贴板中,如NSString:

UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:@"复制的字符串内容"];

return (action == @selector(copy:) || action == @selector(paste:) );

@implementation CopyView-(UIImageView *)img1{ if (_img1 == nil) { _img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)]; NSString* path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"]; _img1.image = [UIImage imageWithContentsOfFile:path]; } return _img1;}-(UIImageView *)img2{ if (_img2 == nil) { _img2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.img1.frame)+50.0f, 20.0f, 100.0f, 100.0f)]; _img2.backgroundColor = [UIColor lightGrayColor]; } return _img2;}- (instancetype)initWithFrame:frame { self = [super initWithFrame:frame]; if  { self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.img1]; [self addSubview:self.img2]; } return self;}-canBecomeFirstResponder{ return YES;}-canPerformAction:action withSender:sender{ NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"]; if ([methodNameArr containsObject:NSStringFromSelector { return YES; } return [super canPerformAction:action withSender:sender];}-copy:sender{ UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setImage:self.img1.image];}-paste:sender{ UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; self.img2.image = [pasteboard image];}-touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self becomeFirstResponder]; UIMenuController* menuController = [UIMenuController sharedMenuController]; [menuController setTargetRect:self.img1.frame inView:self]; [menuController setMenuVisible:YES animated:YES];}@end
  • (nullable NSData )dataForPasteboardType:(NSString)pasteboardType;
    /data类型的多寡放在粘贴板中,pasteboardType同上/
  • (void)setData:(NSData )data forPasteboardType:(NSString)pasteboardType;
    /从粘贴板中收取data/
  • (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
    4.接纳办法
    在ios中,支持UIPasteboard原生控件唯有UITextField、UITextView、UIWebView那多少个,若是想自定义三个控件能够运用UIPasteboard,要求在概念的时候重载 -(BOOL)canBecomeFirstResponder和 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender那多个办法,前者设置控件可称为第一响应器,前面一个决定以此控件能够选用复制、剪切、选中、全选、粘贴等的哪种或三种功用,同样重视载对应的-(void)copy:(id)sender、-(void)cut:(id)sender、-(void)select:(id)sender、-(void)selectAll:(id)sender、-(void)paste:(id)sender方法,在那多少个法子中处监护人件,UIMenuController担当显示UI。
    5.复制图片的轻易例子
    创设三个CopyView

//获取或安装剪切板中的图片数组

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow) name:UIMenuControllerWillShowMenuNotification object:nil];

UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];[pasteboard setString:@"复制的字符串内容"];

/*通过名称获取粘贴板并且移除*/+ removePasteboardWithName:(NSString *)pasteboardName; /*从粘贴板中获取数据,pasteboardType是自定义的,说明app可以处理哪种类型的数据*/- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; /*data类型的数据放在粘贴板中,pasteboardType同上*/- setData:data forPasteboardType:(NSString *)pasteboardType;/*从粘贴板中取出data*/- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;

import "CopyView.h"

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    CopyView* cv = [[CopyView alloc] initWithFrame:self.view.bounds];
    self.view = cv;
    }

@end

[menuController setTargetRect:self.img1.frame inView:self];

@property(readonly,nonatomic) NSInteger numberOfItems;

必威 1显示屏快速照相二零一六-01-19 深夜4.21.09.png必威 2荧屏快速照相二零一六-01-19 凌晨4.21.33.png

import "CopyView.h"

@interface CopyView ()
@property (strong, nonatomic) UIImageView* img1;
@property (strong, nonatomic) UIImageView* img2;
@end

@implementation CopyView
-(UIImageView )img1{
if (_img1 == nil) {
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
NSString
path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"];
_img1.image = [UIImage imageWithContentsOfFile:path];
}
return _img1;
}

-(UIImageView *)img2{
if (_img2 == nil) {
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.img1.frame)+50.0f, 20.0f, 100.0f, 100.0f)];
_img2.backgroundColor = [UIColor lightGrayColor];
}
return _img2;
}

  • (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.img1];
    [self addSubview:self.img2];
    }
    return self;
    }

-(BOOL)canBecomeFirstResponder{
return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"];
if ([methodNameArr containsObject:NSStringFromSelector(action)]) {
return YES;
}
return [super canPerformAction:action withSender:sender];
}

-(void)copy:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.img1.image];
}

-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
self.img2.image = [pasteboard image];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent )event{
[self becomeFirstResponder];
UIMenuController
menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:self.img1.frame inView:self];
[menuController setMenuVisible:YES animated:YES];
}

@end
在controller中

@property(nullable,nonatomic,copy) NSArray*colors;

//获取一组数据对象蕴含的数据类型

创建一个CopyView#import "CopyView.h"@interface CopyView ()@property (strong, nonatomic) UIImageView* img1;@property (strong, nonatomic) UIImageView* img2;@end

//全数颜色数据的类型定义字符串数组

地方方法中好多亟需传入数据类型参数,这么些参数是系统定义好的局地字符窜,如下:

UIPasteboard是ios中寻访粘贴板的原生控件,可分为系统等级的和app品级的,系统品级的独立于app,能够复制二个app的剧情到另三个app;app等第的只可以在app内张开复制和粘贴;它们分别由+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:create那四个类方式开展创办。

//根据数据类型获取一组数据的NSData数据

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

能够复制在粘贴板的数据类型有NSString、UIImage、NSULX570L、UIColor、NSData以及由那几个项目成分构成的数组。可各自由它们的set方法将数据放在粘贴板中,如NSString:

return [super canPerformAction:action withSender:sender];

UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;

    _img1.image = [UIImage imageWithContentsOfFile:path]; 

UIKIT_EXTERN NSArray *UIPasteboardTypeListImage;


//剪切板数据类型键值移除时发送的照看

  NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"];

NSLog(@"您点击的是贴边");

//获取二个自定义的细分板 name参数为此剪切板的称呼 create参数用于安装当以此剪切板不设不常 是还是不是开展创办

}

+ (void)removePasteboardWithName:(NSString *)pasteboardName;

}

@end 

UIKIT_EXTERN NSArray *UIPasteboardTypeListColor;

}

//获取一个自定义的细分板 name参数为此剪切板的称号 create参数用于安装当那个剪切板一纸空文时 是或不是开展创办

- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;

- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet;

@end

UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;

- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;

//剪切板数据类型键值扩充时发送的公告

地方3个初叶化方法,分别赢得或创办3个等第不一致的剪切板,系统级其余剪切板在全体设施中国共产党享,就是应用程序被删掉,其向系统级的剪切板中写入的数目如故在。自定义的剪切板通过一个特定的名称字符串进行创办,它在应用程序内照旧一模一样开拓者开拓的其他应用程序中能够进行数量分享。第一个方法创设的剪切板等价为利用第1个法子创制的剪切板,只是其名目字符串为nil,它平时用于当前选拔内部。

if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {

@implementation ViewController

self.leftImageView.userInteractionEnabled = YES;

相对来讲于地点两组方法,上面那么些方式尤其面向对象,在开荒中利用进一步方便与飞快:

- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;


@end

UIKIT_EXTERN NSArray*UIPasteboardTypeListString;

UIImage *image = [pasteboard image];

UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;

// An empty implementation adversely affects performance during animation.

#import "CopyView.h"

-(void)selectAll:(id)sender {

@end

//当剪切板被剔除时,监听通知,可处理相应事件;

//将剪切板中新型数据对象某一类其余值收取

@property(nullable,nonatomic,copy) UIImage *image;

[menuController setMenuVisible:YES animated:YES];

//增加一组数据对象

+ (UIPasteboard *)pasteboardWithUniqueName;

}

 }  

参照来自:小编山水域  

//将剪切板脊椎结核行数据对象某一类别的数目收取

- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;

三、复制图片的简短例子

@property(nullable,nonatomic,copy) NSURL *URL;

- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;

@property(nullable,nonatomic,copy) NSArray *colors;

//获取或安装剪切板中的颜色数据

self.image = image;

@property(nullable,nonatomic,copy) NSArray*strings;

}

//获取一组数据对象包蕴的数据类型

@property(nullable,nonatomic,copy) NSArray *URLs;

@property (strong, nonatomic) UIImageView* img1;

-(void)paste:(id)sender{

本文由必威发布于必威-编程,转载请注明出处:app等级的只能在app内进行复制和粘贴,必威一、

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。