ios開發(fā) UIPasteboard 的簡單使用

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ù)組??煞謩e由它們的set方法將數(shù)據(jù)放在粘貼板中,如NSString:

UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:@"復(fù)制的字符串內(nèi)容"];

3.認(rèn)識常用方法

/*通過名稱獲取粘貼板并且移除*/
+ (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負(fù)責(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

效果展示

屏幕快照 2016-01-19 下午4.21.09.png
屏幕快照 2016-01-19 下午4.21.33.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容