IOS學習筆記37——UIPasteboard/UIMenuController(三)

來源:互聯網
上載者:User

這篇咱總結總結自訂剪貼簿的使用

其實自訂剪貼簿也非常簡單,無非是放開回應時間,通過UIMenuController自訂剪貼簿,然後就是最關鍵的實現你所用的copy方法拉。

為了方便實用,我給cell添加了長按事件,看代碼---


UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

    [cell addGestureRecognizer:recognizer];

好,事件加上了,剩下的點擊就是實現自訂剪貼簿了,上代碼:


- (void)longPress:(UILongPressGestureRecognizer *)recognizer{

       if (recognizer.state == UIGestureRecognizerStateBegan) {

             CopyCell *cell = (CopyCell *)recognizer.view;

             [cell becomeFirstResponder];

            UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag"action:@selector(flag:)];

            UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve"action:@selector(approve:)];

            UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny"action:@selector(deny:)];

            UIMenuController *menu = [UIMenuController sharedMenuController];

 


        [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];

 


        NSLog(@".....%@",NSStringFromCGRect(cell.frame));

        [menu setTargetRect:cell.frame inView:cell.superview];

 


        [menu setMenuVisible:YES animated:YES];

      }

}

通過上面這段代碼,自訂剪貼簿成功搞定,運行,你猛然會發現,操,怎麼剪貼簿沒有出來,我猜你已經想到為什麼了,因為
- (BOOL)canBecomeFirstResponder{

    return YES;

}

這麼重要的一句話沒有加,OK,下面就是你所要使用的方法了

- (void)flag:(id)sender {

 


    NSLog(@"Cell was flagged");

 


}

- (void)approve:(id)sender {

 


    NSLog(@"Cell was approved");

}

 


- (void)deny:(id)sender {

 


    NSLog(@"Cell was denied");

 


}

好,這就是基本的剪貼簿的自訂使用了,為了更直觀,上述三篇文章均由下面相似的一張

 

 
 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.