How can I get the standard iPhone Copy bubble to appear on a UIImage?

拟墨画扇 提交于 2019-12-03 13:34:16

问题


In iPhoto, I can simply hold my finger over an image to get a "Copy" popup (like the popup you see in text boxes).

In my UIImageView's, this is not the case. How can I enable it?


回答1:


You can manually display the Cut / Copy / Paste menu using the UIMenuController class. For example, the following code will display the menu, centered on your image:

[self becomeFirstResponder];

UIMenuController *copyMenuController = [UIMenuController sharedMenuController];

[copyMenuController setTargetRect:image.frame inView:self.view];
[copyMenuController setMenuVisible:YES animated:YES];

This assumes that you'll be implementing this code in a UIViewController for the view that hosts your image.

To enable the various menu items, you'll also need to implement a few delegate methods in your controller:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{   
    if (action == @selector(cut:))
        return NO;
    else if (action == @selector(copy:))
        return YES;
    else if (action == @selector(paste:))
        return NO;
    else if (action == @selector(select:) || action == @selector(selectAll:)) 
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

In this case, only the Copy menu option will be enabled. You'll also need to implement the appropriate -copy: method to handle what happens when the user selects that menu item.



来源:https://stackoverflow.com/questions/1534886/how-can-i-get-the-standard-iphone-copy-bubble-to-appear-on-a-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!