最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS開發(fā)UIPasteboard類的粘貼板全面詳解

 更新時(shí)間:2023年06月12日 11:44:01   作者:山水域  
這篇文章主要為大家介紹了IOS開發(fā)UIPasteboard類的粘貼板全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

UIPasteboard 特點(diǎn)和用法

UIPasteboard 是 Swift 中用于存儲(chǔ)和檢索應(yīng)用程序中剪貼板中的數(shù)據(jù)的一個(gè)類。剪貼板是應(yīng)用程序之間共享數(shù)據(jù)的一種機(jī)制,UIPasteboard 提供了一種簡(jiǎn)單的方式來存儲(chǔ)和檢索應(yīng)用程序中的剪貼板數(shù)據(jù)。

下面是 UIPasteboard 的一些特點(diǎn)和用法:

  • UIPasteboard 是一個(gè)公共類別,因此可以從所有應(yīng)用程序中訪問。
  • UIPasteboard 包含兩個(gè)方法:setString:和 stringForType:,用于將字符串添加到剪貼板中或從剪貼板中檢索字符串。
  • UIPasteboard 還包含一些類型和方法,如 URL 類型、data 類型、image 類型和 text 類型,用于存儲(chǔ)和檢索不同類型的數(shù)據(jù)。
  • 可以使用 UIPasteboard 將數(shù)據(jù)添加到剪貼板中,例如將一個(gè) URL 添加到剪貼板中,以便在其他應(yīng)用程序中打開該 URL。
  • 可以使用 UIPasteboard 檢索剪貼板中的數(shù)據(jù),例如檢索剪貼板中的 URL,或檢索剪貼板中的文本。
  • UIPasteboard 還提供了一些方法,用于檢查剪貼板中是否有特定類型的數(shù)據(jù),例如檢查剪貼板中是否有文本或 URL。
    下面是一個(gè)簡(jiǎn)單的示例,演示如何使用 UIPasteboard 存儲(chǔ)和檢索剪貼板中的數(shù)據(jù):
let string = "Hello, World!"  
let pasteboard = UIPasteboard.general
pasteboard.setString(string, forType: .text)  
print("剪貼板中的內(nèi)容是:\(string)")
if let string = pasteboard.string {  
    print("剪貼板中的內(nèi)容是:\(string)")  
}

在iOS中,UITextField、UITextView和UIWebView等都有復(fù)制粘貼等功能。而其她控件卻沒有集成這些方便操作的功能。下面我將通過對(duì)粘貼板UIPasteboard這個(gè)類來詳細(xì)說明在iOS中粘貼板的使用方法。

1、剪切板管理類UIPasteboard詳解

UIPasteboard類有3個(gè)初始化方法,如下:

//獲取系統(tǒng)級(jí)別的剪切板
+ (UIPasteboard *)generalPasteboard;
//獲取一個(gè)自定義的剪切板 name參數(shù)為此剪切板的名稱 create參數(shù)用于設(shè)置當(dāng)這個(gè)剪切板不存在時(shí) 是否進(jìn)行創(chuàng)建
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
//獲取一個(gè)應(yīng)用內(nèi)可用的剪切板
+ (UIPasteboard *)pasteboardWithUniqueName;

上面3個(gè)初始化方法,分別獲取或創(chuàng)建3個(gè)級(jí)別不同的剪切板,下面我們?cè)斀庖幌略谑裁辞闆r下用哪種初始化方法

+ (UIPasteboard *)generalPasteboard;系統(tǒng)級(jí)別的剪切板在整個(gè)設(shè)備中共享,即是應(yīng)用程序被刪掉,其向系統(tǒng)級(jí)的剪切板中寫入的數(shù)據(jù)依然在。

+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;自定義的剪切板通過一個(gè)特定的名稱字符串進(jìn)行創(chuàng)建,它在應(yīng)用程序內(nèi)或者同一開發(fā)者開發(fā)的其他應(yīng)用程序中可以進(jìn)行數(shù)據(jù)共享。

舉個(gè)例子:比如你開發(fā)了多款應(yīng)用,用戶全部下載了,在A應(yīng)用中用戶拷貝了一些數(shù)據(jù)(為了數(shù)據(jù)安全,不用系統(tǒng)級(jí)別的Pasteboard),在打開B應(yīng)用時(shí)就會(huì)自動(dòng)識(shí)別,提高用戶體驗(yàn)。

+ (UIPasteboard *)pasteboardWithUniqueName;第3個(gè)方法創(chuàng)建的剪切板等價(jià)為使用第2個(gè)方法創(chuàng)建的剪切板,只是其名稱字符串為nil,它通常用于當(dāng)前應(yīng)用內(nèi)部。(當(dāng)然也可以跨應(yīng)用使用,但必須Bundle Identifier 例com.maoshaoqian.** 星號(hào)前部一樣)

注意:使用第3個(gè)方法創(chuàng)建的剪切板默認(rèn)是不進(jìn)行數(shù)據(jù)持久化的,及當(dāng)應(yīng)用程序退出后,剪切板中內(nèi)容將別抹去。若要實(shí)現(xiàn)持久化,需要設(shè)置persistent屬性為YES。

下面我們來看一下UIPasteboard的常用屬性

//剪切板的名稱
@property(readonly,nonatomic) NSString *name;
//根據(jù)名稱刪除一個(gè)剪切板
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
//是否進(jìn)行持久化
@property(getter=isPersistent,nonatomic) BOOL persistent;
//此剪切板的改變次數(shù) 系統(tǒng)級(jí)別的剪切板只有當(dāng)設(shè)備重新啟動(dòng)時(shí) 這個(gè)值才會(huì)清零
@property(readonly,nonatomic) NSInteger changeCount;

UIPasteboard數(shù)據(jù)類型判斷及其存取

//獲取剪切板中最新數(shù)據(jù)的類型
- (NSArray<NSString *> *)pasteboardTypes;
//獲取剪切板中最新數(shù)據(jù)對(duì)象是否包含某一類型的數(shù)據(jù)
- (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes;
//將剪切板中最新數(shù)據(jù)對(duì)象某一類型的數(shù)據(jù)取出
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
//將剪切板中最新數(shù)據(jù)對(duì)象某一類型的值取出
- (nullable id)valueForPasteboardType:(NSString *)pasteboardType;
//為剪切板中最新數(shù)據(jù)對(duì)應(yīng)的某一數(shù)據(jù)類型設(shè)置值
- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;
//為剪切板中最新數(shù)據(jù)對(duì)應(yīng)的某一數(shù)據(jù)類型設(shè)置數(shù)據(jù)
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;

多組數(shù)據(jù)對(duì)象的存?。?/p>

//數(shù)據(jù)組數(shù)
@property(readonly,nonatomic) NSInteger numberOfItems;
//獲取一組數(shù)據(jù)對(duì)象包含的數(shù)據(jù)類型
- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet;
//獲取一組數(shù)據(jù)對(duì)象中是否包含某些數(shù)據(jù)類型
- (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)對(duì)象
- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的值
- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的NSData數(shù)據(jù)
- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//所有數(shù)據(jù)對(duì)象
@property(nonatomic,copy) NSArray *items;
//添加一組數(shù)據(jù)對(duì)象
- (void)addItems:(NSArray<NSDictionary<NSString *, id> *> *)items;

上面方法中很多需要傳入數(shù)據(jù)類型參數(shù),這些參數(shù)是系統(tǒng)定義好的一些字符竄,如下:

//所有字符串類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListString;
//所有URL類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListURL;
//所有圖片數(shù)據(jù)的類型定義字符串?dāng)?shù)據(jù)
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListImage;
//所有顏色數(shù)據(jù)的類型定義字符串?dāng)?shù)組
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListColor;

相比于上面兩組方法,下面這些方法更加面向?qū)ο螅陂_發(fā)中使用更加方便與快捷:

//獲取或設(shè)置剪切板中的字符串?dāng)?shù)據(jù)
@property(nullable,nonatomic,copy) NSString *string;
//獲取或設(shè)置剪切板中的字符串?dāng)?shù)組
@property(nullable,nonatomic,copy) NSArray<NSString *> *strings;
//獲取或設(shè)置剪切板中的URL數(shù)據(jù)
@property(nullable,nonatomic,copy) NSURL *URL;
//獲取或設(shè)置剪切板中的URL數(shù)組
@property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs;
//獲取或s何止剪切板中的圖片數(shù)據(jù)
@property(nullable,nonatomic,copy) UIImage *image;
//獲取或設(shè)置剪切板中的圖片數(shù)組
@property(nullable,nonatomic,copy) NSArray<UIImage *> *images;
//獲取或設(shè)置剪切板中的顏色數(shù)據(jù)
@property(nullable,nonatomic,copy) UIColor *color;
//獲取或設(shè)置剪切板中的顏色數(shù)組
@property(nullable,nonatomic,copy) NSArray<UIColor *> *colors;
//部分代碼參考
- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        //action 會(huì)返回很多,想用哪個(gè)就寫那個(gè)(action == @selector(cut:) )
    return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setImage:self.image];
    if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {
        [self.delegate transSomeTing:pasteboard.image];
        NSLog(@"%@",self.image);
    }
    NSLog(@"您點(diǎn)擊的是拷貝%@",pasteboard.items);
}
-(void)paste:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    UIImage *image = [pasteboard image];
    if (image) {
        self.image = image;
    }
    NSLog(@"您點(diǎn)擊的是粘貼");
}
- (void)cut:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setImage:self.image];
    NSLog(@"您點(diǎn)擊的是剪切");
}
- (void)select:(id)sender {
    NSLog(@"您點(diǎn)擊的是選擇");
}
-(void)selectAll:(id)sender {
    NSLog(@"您點(diǎn)擊的是全選");
}

對(duì)剪切板的某些操作會(huì)觸發(fā)如下通知:

//剪切板內(nèi)容發(fā)生變化時(shí)發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;
//剪切板數(shù)據(jù)類型鍵值增加時(shí)發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;
//剪切板數(shù)據(jù)類型鍵值移除時(shí)發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey;
//剪切板被刪除時(shí)發(fā)送的通知
UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;
//使用舉例
//當(dāng)剪切板被刪除時(shí),監(jiān)聽通知,可處理相應(yīng)事件;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide) name:UIPasteboardRemovedNotification object:nil];

2、剪切板管理類UIPasteboard具體使用

我們以系統(tǒng)粘貼板+ (UIPasteboard *)generalPasteboard;來舉例子我們給UIImageView添加復(fù)制粘貼事件

//  ViewController.m
//  Practice_UIPasteboard
//
//
#import "ViewController.h"
#import "PasteboardLabel.h"
#import "PasteboardImageView.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()<transSometing>
@property (strong, nonatomic) IBOutlet PasteboardImageView *leftImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftLabel.userInteractionEnabled = YES;
//用于監(jiān)聽 UIMenuController的變化
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow) name:UIMenuControllerWillShowMenuNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide) name:UIMenuControllerWillHideMenuNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)longPressGestureAction:(UILongPressGestureRecognizer *)sender {
//要將圖片變?yōu)榈谝豁憫?yīng)者,而且要把圖片設(shè)為**可交換狀態(tài)**
        [self.leftImageView becomeFirstResponder];
        self.leftImageView.userInteractionEnabled = YES;
        self.leftImageView.delegate = self;
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        [menuController setTargetRect:self.leftImageView.frame inView:self.view];
        [menuController setMenuVisible:YES animated:YES];
    }
}
//  PasteboardImageView.m
//  Practice_UIPasteboard
#import "PasteboardImageView.h"
@implementation PasteboardImageView
//這個(gè)方法不能少
- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setImage:self.image];
    if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {
        [self.delegate transSomeTing:pasteboard.image];
        NSLog(@"%@",self.image);
    }
    NSLog(@"您點(diǎn)擊的是拷貝%@",pasteboard.items);
}
-(void)paste:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    UIImage *image = [pasteboard image];
    if (image) {
        self.image = image;
    }
    NSLog(@"您點(diǎn)擊的是粘貼");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
@end

以上就是IOS開發(fā)UIPasteboard類的粘貼板全面詳解的詳細(xì)內(nèi)容,更多關(guān)于IOS開發(fā)UIPasteboard粘貼板的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • iOS中類似微信紅點(diǎn)顯示功能

    iOS中類似微信紅點(diǎn)顯示功能

    ios中類似微信紅點(diǎn)顯示功能,設(shè)計(jì)思路非常簡(jiǎn)單,給UIView增加一個(gè)分類 所有的視圖都可以根據(jù)需要來進(jìn)行紅點(diǎn)顯示。下面通過實(shí)例代碼看下實(shí)現(xiàn)方法吧
    2016-12-12
  • IOS中一段文字設(shè)置多種字體顏色代碼

    IOS中一段文字設(shè)置多種字體顏色代碼

    這篇文章主要介紹了IOS中一段文字設(shè)置多種字體顏色代碼,十分的實(shí)用,有需要的小伙伴可以參考下。
    2015-05-05
  • ios的collection控件的自定義布局實(shí)現(xiàn)與設(shè)計(jì)

    ios的collection控件的自定義布局實(shí)現(xiàn)與設(shè)計(jì)

    這篇文章主要介紹了mac、iOS端支持自定義布局的collection控件的實(shí)現(xiàn)與設(shè)計(jì),需要的朋友學(xué)習(xí)參考下吧。
    2017-12-12
  • iOS如何獲取手機(jī)的Mac地址

    iOS如何獲取手機(jī)的Mac地址

    這篇文章主要為大家詳細(xì)介紹了iOS獲取手機(jī)的Mac地址的多種方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • IOS10 配置權(quán)限詳細(xì)介紹

    IOS10 配置權(quán)限詳細(xì)介紹

    這篇文章主要介紹了IOS10 配置權(quán)限詳細(xì)介紹的相關(guān)資料,訪問相機(jī)、相冊(cè)、麥克風(fēng)、通訊錄等等權(quán)限,都需要進(jìn)行相關(guān)的配置,不然會(huì)直接crash,需要的朋友可以參考下
    2016-12-12
  • IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

    這篇文章主要介紹了IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • iOS中各種UI控件屬性設(shè)置示例代碼

    iOS中各種UI控件屬性設(shè)置示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS中各種UI控件屬性設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • IOS 開發(fā)之Swift 元組的實(shí)例詳解

    IOS 開發(fā)之Swift 元組的實(shí)例詳解

    這篇文章主要介紹了IOS 開發(fā)之Swift 元組的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • IOS 中UIKit-UIPageControl利用delegate定位圓點(diǎn)位置

    IOS 中UIKit-UIPageControl利用delegate定位圓點(diǎn)位置

    這篇文章主要介紹了IOS 中UIKit-UIPageControl利用delegate定位圓點(diǎn)位置 的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解

    iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解

    倒計(jì)時(shí)在我們?nèi)粘i_發(fā)中必不可少,最近在公司的一個(gè)項(xiàng)目中就遇到了這個(gè)需求,本文著重介紹的是利用iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04

最新評(píng)論

成安县| 奈曼旗| 莒南县| 剑阁县| 林州市| 嘉祥县| 昌黎县| 浦北县| 昭通市| 丰镇市| 海城市| 肇东市| 昌图县| 石楼县| 定陶县| 四平市| 曲沃县| 左云县| 乌鲁木齐市| 建昌县| 高平市| 苗栗县| 巴里| 全椒县| 丹阳市| 长丰县| 中西区| 上犹县| 通州区| 名山县| 应城市| 张家口市| 桓仁| 冀州市| 德江县| 龙江县| 象州县| 彩票| 潜江市| 普洱| 合江县|