我真的经历了与此主题相关的所有其他问题。但到目前为止,没有人帮助我。
我想在UIMenuController
上显示UIView
。因此,我将UIView
子类化,并且已经实现了所有必需的方法。
在UIViewController
我实例化UIGestureRecognizer
并将其添加到自定义UIView
。这很好用。我也可以处理长按手势。只是UIMenuController
不会出现!
@interface CopyableView : UIView
@end
@implementation CopyableView
- (BOOL)canBecomeFirstResponder
{
NSLog(@"_CAN BECOME 1. RESPONDER_");
return YES;
}
- (BOOL)canPerformAction:(SEL)action
withSender:(id)sender
{
NSLog(@"_CAN PERFORM ACTION_");
return (action == @selector(copy:));
}
- (void)copy:(id)sender
{
NSLog(@"_COPY_");
}
@end
@interface InfoViewController : UIViewController
@property (nonatomic, weak) IBOutlet CopyableView *myView;
@end
@implementation InfoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPressGesture:)]];
}
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"__ %i __", [UIMenuController sharedMenuController].menuVisible);
[self.myView becomeFirstResponder];
UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Copy"
action:@selector(copy:)];
[[UIMenuController sharedMenuController] setMenuItems:@[item]];
[[UIMenuController sharedMenuController] setTargetRect:CGRectMake(33, 33, 33, 33)
inView:self.myView];
[[UIMenuController sharedMenuController] setMenuVisible:YES
animated:NO];
NSLog(@"__ %i __", [UIMenuController sharedMenuController].menuVisible);
NSLog(@"#### %@ ####", NSStringFromCGRect([UIMenuController sharedMenuController].menuFrame));
}
}
检查[UIMenuController sharedMenuController].menuVisible
会在方法结束时返回true
。所以事情似乎有效。我仍然没有看到任何事情发生。
UIMenuController
的框架是{{0, 0}, {0, 0}}
,我无法理解。