1.概述
UIPasteboard是ios中訪問粘貼板的原生控件畏陕,可分為系統(tǒng)等級的和app等級的拓劝,系統(tǒng)等級的獨立于app,可以復(fù)制一個app的內(nèi)容到另一個app踱卵;app等級的只能在app內(nèi)進行復(fù)制和粘貼廊驼;它們分別由+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create
這兩個類方法進行創(chuàng)建。
2.數(shù)據(jù)類型
可以復(fù)制在粘貼板的數(shù)據(jù)類型有NSString惋砂、UIImage妒挎、NSURL、UIColor班利、NSData以及由這些類型元素組成的數(shù)組饥漫≌ゴ簦可分別由它們的set方法將數(shù)據(jù)放在粘貼板中罗标,如NSString:
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:@"復(fù)制的字符串內(nèi)容"];
3.認識常用方法
/*通過名稱獲取粘貼板并且移除*/
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
/*從粘貼板中獲取數(shù)據(jù),pasteboardType是自定義的积蜻,說明app可以處理哪種類型的數(shù)據(jù)*/
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
/*data類型的數(shù)據(jù)放在粘貼板中闯割,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
這兩個方法丙笋,前者設(shè)置控件可稱為第一響應(yīng)器谢澈,后者決定這個控件能夠使用復(fù)制、剪切御板、選中锥忿、全選、粘貼等的哪一種或幾種功能,并重載對應(yīng)的-(void)copy:(id)sender
怠肋、-(void)cut:(id)sender
敬鬓、-(void)select:(id)sender
、-(void)selectAll:(id)sender
笙各、-(void)paste:(id)sender
方法钉答,在這幾個方法中處理事件,UIMenuController負責(zé)顯示UI。
5.復(fù)制圖片的簡單例子
創(chuàng)建一個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
效果展示