自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)應用剪貼板功能教程

移動開發(fā) iOS
本文介紹的是IOS應用中的剪貼板的功能實現(xiàn),很詳細的講解了所使用的類和控件等內(nèi)容,代碼實現(xiàn),來看詳細內(nèi)容。

iOS開發(fā)應用剪貼板功能教程是本文要介紹的內(nèi)容,在iOS中,可以使用剪貼板實現(xiàn)應用程序之中以及應用程序之間實現(xiàn)數(shù)據(jù)的共享。比如你可以從iPhone QQ復制一個url,然后粘貼到safari瀏覽器中查看這個鏈接的內(nèi)容。

一、在iOS中下面三個控件,自身就有復制-粘貼的功能:

1、UITextView

2、UITextField

3、UIWebView

二、UIKit framework提供了幾個類和協(xié)議方便我們在自己的應用程序中實現(xiàn)剪貼板的功能。

1、UIPasteboard:我們可以向其中寫入數(shù)據(jù),也可以讀取數(shù)據(jù)

2、UIMenuController:顯示一個快捷菜單,用來復制、剪貼、粘貼選擇的項。

3、UIResponder中的 canPerformAction:withSender:用于控制哪些命令顯示在快捷菜單中。

4、當快捷菜單上的命令點擊的時候,UIResponderStandardEditActions將會被調(diào)用。

三、下面這些項能被放置到剪貼板中

1、UIPasteboardTypeListString —  字符串數(shù)組, 包含kUTTypeUTF8PlainText

2、UIPasteboardTypeListURL —   URL數(shù)組,包含kUTTypeURL

3、UIPasteboardTypeListImage —   圖形數(shù)組, 包含kUTTypePNG 和kUTTypeJPEG

4、UIPasteboardTypeListColor —   顏色數(shù)組

四、剪貼板的類型分為兩種:

系統(tǒng)級:使用UIPasteboardNameGeneral和UIPasteboardNameFind創(chuàng)建,系統(tǒng)級的剪貼板,當應用程序關(guān)閉,或者卸載時,數(shù)據(jù)都不會丟失。

應用程序級:通過設置,可以讓數(shù)據(jù)在應用程序關(guān)閉之后仍然保存在剪貼板中,但是應用程序卸載之后數(shù)據(jù)就會失去。我們可用通過pasteboardWithName:create:來創(chuàng)建。

了解這些之后,下面通過一系列的例子來說明如何在應用程序中使用剪貼板。

例子:

1、復制剪貼文本。

下面通過一個例子,可以在tableview上顯示一個快捷菜單,上面只有復制按鈕,復制tableview上的數(shù)據(jù)之后,然后粘貼到title上。

定義一個單元格類CopyTableViewCell,在這個類的上顯示快捷菜單,實現(xiàn)復制功能。

  1. @interface CopyTableViewCell : UITableViewCell {     
  2.  id delegate;}@property (nonatomic, retain) id delegate;  
  3.  @end 

實現(xiàn)CopyTableViewCell :

  1. #import "CopyTableViewCell.h"@implementation CopyTableViewCell@synthesize delegate;  
  2. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier   
  3. {     
  4.  if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { }    
  5.    return self;  
  6. }  
  7. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {   
  8.    [super setSelected:selected animated:animated];  
  9. }  
  10. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {    
  11.   [[self delegate] performSelector:@selector(showMenu:)                
  12.            withObject:self afterDelay:0.9f];         
  13.            [super setHighlighted:highlighted animated:animated];  
  14. }  
  15. - (BOOL)canBecomeFirstResponder {   
  16.    return YES;  
  17. }  
  18. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{   
  19.    if (action == @selector(cut:)){   
  20.     return NO;  
  21. }  else if(action == @selector(copy:)){      
  22.     return YES;    
  23.  }       
  24.  else if(action == @selector(paste:)){   
  25.     return NO;    
  26. }       
  27. else if(action == @selector(select:)){     
  28.      return NO;    
  29.  }      
  30.   else if(action == @selector(selectAll:)){      
  31.  return NO;    
  32. }    else  {     
  33.   return [super canPerformAction:action withSender:sender];    
  34. }  
  35. }  
  36. - (void)copy:(id)sender {    
  37.   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];     
  38.   [pasteboard setString:[[self textLabel]text]];  
  39.  }  
  40. - (void)dealloc {    
  41.   [super dealloc];  
  42. }  
  43. @end 

定義CopyPasteTextController,實現(xiàn)粘貼功能。

  1. @interface CopyPasteTextController : UIViewController<UITableViewDelegate>   
  2. {      
  3. //用來標識是否顯示快捷菜單      
  4. BOOL menuVisible;      
  5. UITableView *tableView;  
  6. }  
  7. @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;  
  8. @property (nonatomic, retain) IBOutlet UITableView *tableView;  
  9. @end  

實現(xiàn)CopyPasteTextController :

  1. #import "CopyPasteTextController.h"  
  2. #import "CopyTableViewCell.h"  
  3. @implementation CopyPasteTextController  
  4. @synthesize menuVisible,tableView;  
  5. - (void)viewDidLoad {      
  6. [super viewDidLoad];     
  7.  [self setTitle:@"文字復制粘貼"]; //點擊這個按鈕將剪貼板的內(nèi)容粘貼到title上      
  8.  UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]               
  9.   initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh      
  10.     target:self  
  11.      action:@selector(readFromPasteboard:)]  
  12.      autorelease];      
  13.  [[self navigationItem] setRightBarButtonItem:addButton];  
  14.  }  
  15.  // Customize the number of sections in the table view.  
  16.  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{   
  17.     return 1;  
  18.  }  
  19.  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
  20.    return 9;}// Customize the appearance of table view cells.  
  21.    - (UITableViewCell *)tableView:(UITableView *)tableView   
  22.    cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
  23.   static NSString *CellIdentifier =@"Cell";    
  24.     CopyTableViewCell *cell = (CopyTableViewCell *)  
  25.     [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];    
  26.       if (cell == nil)     {        
  27.         cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
  28.               [cell setDelegate:self];  
  29.       }        
  30.         // Configure the cell.     
  31.       NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];   
  32.          [[cell textLabel] setText:text];     
  33.           return cell;  
  34. }  
  35. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    
  36.   if([self isMenuVisible])    {        
  37.     return;   
  38.  }     
  39.   [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES                           
  40.                                  animated:YES];}//顯示菜單- (void)showMenu:(id)cell {    
  41.     if ([cell isHighlighted]) {      
  42.         [cell becomeFirstResponder];  
  43.        UIMenuController * menu = [UIMenuController sharedMenuController];  
  44.       [menu setTargetRect: [cell frame] inView: [self view]];   
  45.    [menu setMenuVisible: YES animated: YES];  
  46.  }  
  47.  }  
  48. - (void)readFromPasteboard:(id)sender {    
  49.   [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",[[UIPasteboard generalPasteboard] string]]];  
  50. }  
  51. - (void)didReceiveMemoryWarning{     
  52.  // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];   
  53.         // Relinquish ownership any cached data, images, etc that aren't in use.  
  54.     }  
  55.  - (void)viewDidUnload{      
  56.  [super viewDidUnload];     
  57.   [self.tableView release];   
  58.   // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.     
  59.    // For example: self.myOutlet = nil;  

效果:

iOS開發(fā)應用剪貼板功能教程

復制一行數(shù)據(jù):

iOS開發(fā)應用剪貼板功能教程

點擊右上角的按鈕粘貼,將數(shù)據(jù)顯示在title上:

#p#

2、圖片復制粘貼

下面通過一個例子,將圖片復制和剪貼到另外一個UIImageView中間。

1、在界面上放置兩個uiimageview,一個是圖片的數(shù)據(jù)源,一個是將圖片粘貼到的地方。CopyPasteImageViewController 代碼如下:

  1. @interface CopyPasteImageViewController : UIViewController {     
  2.  UIImageView *imageView;      
  3.  UIImageView *pasteView;     
  4.   UIImageView *selectedView;  
  5. }@property (nonatomic, retain) IBOutlet UIImageView *imageView;  
  6. @property (nonatomic, retain) IBOutlet UIImageView *pasteView;  
  7. @property (nonatomic, retain) UIImageView *selectedView;  
  8. - (void)placeImageOnPasteboard:(id)view;  
  9. @end 

2、當觸摸圖片的時候我們顯示快捷菜單:

  1. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {      
  2. NSSet *copyTouches = [event touchesForView:imageView];      
  3. NSSet *pasteTouches = [event touchesForView:pasteView];          
  4. [self becomeFirstResponder];      
  5. if ([copyTouches count] > 0) {     
  6.      [self performSelector:@selector(showMenu:)   
  7.       withObject:imageView afterDelay:0.9f];   
  8.  }    
  9.    else  if([pasteTouches count] > 0) {    
  10.          [self performSelector:@selector(showMenu:)          
  11.         withObject:pasteView afterDelay:0.9f];   
  12.  }     
  13.   [super touchesBegan:touches withEvent:event];  
  14. }  
  15. - (void)showMenu:(id)view {   
  16.    [self setSelectedView:view];   
  17.   UIMenuController * menu = [UIMenuController sharedMenuController];    
  18.     [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];     
  19.  [menu setMenuVisible: YES animated: YES];} 

這里的快捷菜單,顯示三個菜單項:剪貼、粘貼、復制:

  1. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
  2.     if (action == @selector(cut:)) {          
  3.     return ([self selectedView] == imageView) ? YES : NO;  
  4.  } else if (action == @selector(copy:)) {          
  5.       return ([self selectedView] == imageView) ? YES : NO;  
  6.  } else if (action == @selector(paste:)) {      
  7.      return ([self selectedView] == pasteView) ? YES : NO;  
  8.  } else if (action == @selector(select:)) {    
  9.        return NO;      
  10. } else if (action == @selector(selectAll:)) {      
  11.     return NO;      
  12.  } else {       
  13.        return [super canPerformAction:action withSender:sender];  
  14.    }  
  15.  }  
  16.  - (void)cut:(id)sender {     
  17.   [self copy:sender];     
  18.    [imageView setHidden:YES];  
  19. }  
  20. - (void)copy:(id)sender {    
  21.   [self placeImageOnPasteboard:[self imageView]];  
  22. }  
  23. - (void)paste:(id)sender {     
  24.  UIPasteboard *appPasteBoard =[UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];      
  25.  NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];     
  26.   pasteView.image = [UIImage imageWithData:data];  

效果:

1、點擊圖片,顯示菜單按鈕。

iOS開發(fā)應用剪貼板功能教程

2、點擊復制,將數(shù)據(jù)復制到剪貼板上:

iOS開發(fā)應用剪貼板功能教程

3、點擊粘貼,將數(shù)據(jù)粘貼到uiimageview上。

iOS開發(fā)應用剪貼板功能教程

小結(jié):iOS開發(fā)應用剪貼板功能教程的內(nèi)容介紹完了,希望通過本文的學習對你有所幫助!

責任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2010-02-02 17:47:59

C++操作剪貼板

2009-12-18 14:10:29

Ruby訪問剪貼板

2024-04-09 08:27:01

Android高效管理數(shù)據(jù)

2022-03-31 22:53:47

Windows 11太陽谷2智能剪貼板

2021-12-05 09:28:18

Windows 11操作系統(tǒng)微軟

2021-12-12 09:42:48

Windows 11桌面微軟

2021-07-29 09:55:59

鴻蒙HarmonyOS應用

2020-12-14 05:57:01

clipboard.Selection execCommand

2009-08-10 17:37:54

2020-07-02 07:53:59

App操作系統(tǒng)應用

2021-12-02 10:11:44

鴻蒙HarmonyOS應用

2016-05-11 15:01:31

Linux剪貼板管理器

2020-10-12 09:40:57

Windows 10Windows操作系統(tǒng)

2021-08-29 07:43:43

CopyQ操作系統(tǒng)微軟

2018-03-23 10:15:28

Windows 10云剪貼板復制粘貼

2023-02-06 07:17:22

2021-08-03 07:06:54

Windows 11操作系統(tǒng)微軟

2009-10-21 10:15:29

VB.NET復制

2020-10-11 13:35:00

Windows 10微軟粘貼

2021-03-09 05:48:01

Windows10操作系統(tǒng)21H2
點贊
收藏

51CTO技術(shù)棧公眾號