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ù)組饥漫≌ゴ簦可分別由它們的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

效果展示

屏幕快照 2016-01-19 下午4.21.09.png
屏幕快照 2016-01-19 下午4.21.33.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杈抢,一起剝皮案震驚了整個濱河市数尿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌惶楼,老刑警劉巖右蹦,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件虏缸,死亡現(xiàn)場離奇詭異,居然都是意外死亡嫩实,警方通過查閱死者的電腦和手機刽辙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甲献,“玉大人宰缤,你說我怎么就攤上這事』稳鳎” “怎么了慨灭?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長球及。 經(jīng)常有香客問我氧骤,道長,這世上最難降的妖魔是什么吃引? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任筹陵,我火速辦了婚禮,結(jié)果婚禮上镊尺,老公的妹妹穿的比我還像新娘朦佩。我一直安慰自己,他們只是感情好庐氮,可當(dāng)我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布语稠。 她就那樣靜靜地躺著,像睡著了一般弄砍。 火紅的嫁衣襯著肌膚如雪仙畦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天音婶,我揣著相機與錄音慨畸,去河邊找鬼。 笑死桃熄,一個胖子當(dāng)著我的面吹牛先口,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瞳收,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼碉京,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了螟深?” 一聲冷哼從身側(cè)響起谐宙,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎界弧,沒想到半個月后凡蜻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體搭综,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年划栓,在試婚紗的時候發(fā)現(xiàn)自己被綠了兑巾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡忠荞,死狀恐怖蒋歌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情委煤,我是刑警寧澤堂油,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站碧绞,受9級特大地震影響府框,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜讥邻,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一迫靖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧计维,春花似錦袜香、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽实抡。三九已至欠母,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吆寨,已是汗流浹背赏淌。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留啄清,地道東北人六水。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像辣卒,于是被迫代替她去往敵國和親掷贾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容