UIMenuController,UIPasteboard:複製,粘貼詳解

來源:互聯網
上載者:User

標籤:複製 粘貼

首先用下面四張圖,說明我要實現的功能。【講一張圖片複製到另外一個UIimageView上】



實現代碼如下:

-(IBAction)panGestureTop:(UILongPressGestureRecognizer *)longPress{     UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@"複製" action:@selector(copyimage)];     UIMenuItem * itemTrans = [[UIMenuItem alloc] initWithTitle:@"轉寄" action:@selector(trans)];     UIMenuItem * itemCollect = [[UIMenuItem alloc] initWithTitle:@"收藏" action:@selector(collect)];     UIMenuItem * itemJoin = [[UIMenuItem alloc] initWithTitle:@"加入" action:@selector(join)];        UIMenuController * menuController = [UIMenuController sharedMenuController];    [menuController setMenuItems: @[itemPase,itemCollect,itemTrans,itemJoin]];        CGPoint location = [longPress locationInView:[longPress view]];    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);    [menuController setTargetRect:menuLocation inView:[longPress view]];    menuController.arrowDirection = UIMenuControllerArrowDown;    [menuController setMenuVisible:YES animated:YES];}-(BOOL)canBecomeFirstResponder{    return YES;}-(void)copyimage{        UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@"com.test.image" create:YES];    pasterBoard.image = [self.topImgView image];    }-(IBAction)panGestureBottom:(UILongPressGestureRecognizer *)longPress{    UIMenuItem * itemPase = [[UIMenuItem alloc] initWithTitle:@"粘貼" action:@selector(pase)];        UIMenuController * menuController = [UIMenuController sharedMenuController];    [menuController setMenuItems: @[itemPase]];        CGPoint location = [longPress locationInView:[longPress view]];    CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);    [menuController setTargetRect:menuLocation inView:[longPress view]];    menuController.arrowDirection = UIMenuControllerArrowDown;        [menuController setMenuVisible:YES animated:YES];}-(void)pase{    UIPasteboard * pasterBoard = [UIPasteboard pasteboardWithName:@"com.test.image" create:NO];    self.bottomImgView.image = pasterBoard.image;}

當操作的時候,我們需要把當前的UIViewController變成第一響應者。


複製相當於copy了一份,加入我在複製過圖片之後,粘貼圖片之前,把原來的那個image刪除掉,則依然可以進行粘貼。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.