将 UITapGestureRecognizer 添加到 UITextView 而不阻止 textView 触摸
Add UITapGestureRecognizer to UITextView without blocking textView touches
如何将 UITapGestureRecognizer
添加到 UITextView,但仍然可以像 normal 那样接触到 UITextView
?
目前,只要我向我的 textView 添加自定义手势,它就会阻止点击 UITextView 默认操作,例如定位光标。
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
textView.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
println("tapped term – but blocking the tap for textView :-/")
…
}
我如何处理点击但保持任何 textView 行为(如光标定位)不变?
要做到这一点,让您的视图控制器采用 UIGestureRecognizerDelegate 并覆盖应该同时识别手势识别方法,例如:
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
tapTerm.delegate = self
textView.addGestureRecognizer(tapTerm)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
如果有人来这里寻找@Zell B. 在 Objective C 中的答案,代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textViewTapped:)];
tap.delegate = self;
tap.numberOfTapsRequired = 1;
[self.textView addGestureRecognizer:tap];
}
- (void)textViewTapped:(UITapGestureRecognizer *)tap {
//DO SOMTHING
}
#pragma mark - Gesture recognizer delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
PS: 别忘了
Swift 4.2
以下步骤允许我通过点击退出全屏 UITextView,同时允许滚动 UITextView 的内容:
- 已断开 UIGestureRecognizer 与 UITableView 的连接。
- 制作了一个 CustomTextView:UITextView。
- 向带有 CustomTextView 的特定 UIViewController 添加了一个 'sender' 变量。
- 'Touches Ended...'
的陷阱
- 在发送者 UIViewController 中调用 excape 函数。
class CustomTextView: UITextView {
var sender: DocViewController?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let controller = sender {
controller.handleSwipeGesture()
}
}
}
我可以滚动 UITextView 的内容,也可以点击退出。
'sender' 在创建时从宿主 UIViewController 设置。
如何将 UITapGestureRecognizer
添加到 UITextView,但仍然可以像 normal 那样接触到 UITextView
?
目前,只要我向我的 textView 添加自定义手势,它就会阻止点击 UITextView 默认操作,例如定位光标。
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
textView.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
println("tapped term – but blocking the tap for textView :-/")
…
}
我如何处理点击但保持任何 textView 行为(如光标定位)不变?
要做到这一点,让您的视图控制器采用 UIGestureRecognizerDelegate 并覆盖应该同时识别手势识别方法,例如:
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
tapTerm.delegate = self
textView.addGestureRecognizer(tapTerm)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
如果有人来这里寻找@Zell B. 在 Objective C 中的答案,代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textViewTapped:)];
tap.delegate = self;
tap.numberOfTapsRequired = 1;
[self.textView addGestureRecognizer:tap];
}
- (void)textViewTapped:(UITapGestureRecognizer *)tap {
//DO SOMTHING
}
#pragma mark - Gesture recognizer delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
PS: 别忘了
Swift 4.2
以下步骤允许我通过点击退出全屏 UITextView,同时允许滚动 UITextView 的内容:
- 已断开 UIGestureRecognizer 与 UITableView 的连接。
- 制作了一个 CustomTextView:UITextView。
- 向带有 CustomTextView 的特定 UIViewController 添加了一个 'sender' 变量。
- 'Touches Ended...' 的陷阱
- 在发送者 UIViewController 中调用 excape 函数。
class CustomTextView: UITextView {
var sender: DocViewController?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let controller = sender {
controller.handleSwipeGesture()
}
}
}
我可以滚动 UITextView 的内容,也可以点击退出。
'sender' 在创建时从宿主 UIViewController 设置。