带有 UIMenucontroller 的 UILabel 不会放弃触摸外部的第一响应者
我对 UILabel 进行了子类化以提供复制菜单,并希望添加某种类型的效果,使 UILabel 在显示此菜单时脱颖而出。
现在我正在尝试添加和删除边框。它工作正常,但是如果用户触摸标签,然后触摸标签外部,则边框不会消失,尽管复制菜单会消失。
添加一些 NSLog 后,发生这种情况时似乎没有调用 resignfirstresponder 。当这种情况发生时,响应者链中会发生什么?我怎样才能让边框在这种情况下消失?
代码如下:
@implementation CopyLabel
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (BOOL)becomeFirstResponder {
if([super becomeFirstResponder]) {
self.highlighted = YES;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
return YES;
}
return NO;
}
- (BOOL)resignFirstResponder {
if([super resignFirstResponder]) {
self.highlighted = NO;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
[menu update];
NSLog(@"Resign");
return true;
}
return false;
}
- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if([self isFirstResponder]) {
[self resignFirstResponder];
}
else if([self becomeFirstResponder]) {
} else {
[self resignFirstResponder];
}
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.layer.borderColor = [UIColor blueColor].CGColor;
self.layer.borderWidth = 0.0;
if(self.highlighted) {
self.layer.borderWidth = 1.0;
}
}
@end
I have subclassed UILabel to provide a copy menu and would like to add some type of effect that makes the UILabel stand out when this menu is displayed.
Right now I am trying to add and remove a border. It works fine however if the user touches the label and then touches outside of the label the border won't disappear although the copy menu does.
After adding some NSLog's it seems like resignfirstresponder is not being called when this occurs. What happens in the responder chain when this happens and how can I get the border to disappear in this event?
Code as follows :
@implementation CopyLabel
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (BOOL)becomeFirstResponder {
if([super becomeFirstResponder]) {
self.highlighted = YES;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
return YES;
}
return NO;
}
- (BOOL)resignFirstResponder {
if([super resignFirstResponder]) {
self.highlighted = NO;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
[menu update];
NSLog(@"Resign");
return true;
}
return false;
}
- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if([self isFirstResponder]) {
[self resignFirstResponder];
}
else if([self becomeFirstResponder]) {
} else {
[self resignFirstResponder];
}
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.layer.borderColor = [UIColor blueColor].CGColor;
self.layer.borderWidth = 0.0;
if(self.highlighted) {
self.layer.borderWidth = 1.0;
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIMenuController 发布 UIMenuControllerDidHideMenuNotification。当您监听该通知(使用 NSNotificationCenter)时,您可以在正确的时间将 resignFirstResponder 发送到您的标签。
例子:
UIMenuController posts a UIMenuControllerDidHideMenuNotification. When you listen for that notification (using NSNotificationCenter) you can send resignFirstResponder to your Label at the right time.
Example: