简体   繁体   English

UITextView 的 UISwipeGestureRecognizer

[英]UISwipeGestureRecognizer for UITextView

I have a UITableView in which each has a custom UITableViewCell.我有一个 UITableView,其中每个都有一个自定义 UITableViewCell。 Inside each of this cell it has a UITextView.在每个单元格内,它都有一个 UITextView。 I would like to wire each of this UITextView to an event using UISwipeGestureRecognizer when the UITextView is tapped once.当点击一次 UITextView 时,我想使用 UISwipeGestureRecognizer 将每个 UITextView 连接到一个事件。 How do I do this?我该怎么做呢?

All you really have to do is adopt the UITextView delegate protocol.您真正需要做的就是采用 UITextView 委托协议。 Make sure your UITextView instances are editable, then the following method will let you do what you need确保你的 UITextView 实例是可编辑的,那么下面的方法会让你做你需要的

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    // call whatever methods you want from here, including the method linked in your gesture recognizer;
    return NO; // this makes sure you don't edit anything, if that is the case you want
}

You should be able to do whatever you want from in there since that line gets called every time you tap an editable textView.您应该可以从那里做任何您想做的事情,因为每次点击可编辑的 textView 时都会调用该行。 If you need to access information about the cell it is in, you can find it through calling superviews.如果你需要访问它所在的单元格的信息,你可以通过调用 superviews 找到它。 For example, if you embedded you textView in the contentView of the cell, textView.superview.superview will return the cell it is in. The first superview call returns the contentView while the second gives you the cell.例如,如果您将 textView 嵌入到单元格的 contentView 中,则textView.superview.superview将返回它所在的单元格。第一个 superview 调用返回 contentView 而第二个为您提供单元格。 Calling another superview would give you the tableView.调用另一个超级视图会给你 tableView。 I hope that helps.我希望这会有所帮助。

EDIT: getting the index path is pretty simple if you can find the cell the textView is embedded in. Assuming you have the textView as a subview of the cell's contentView, this is how you would get the index path.编辑:如果您可以找到嵌入 textView 的单元格,则获取索引路径非常简单。假设您将 textView 作为单元格内容视图的子视图,这就是您获取索引路径的方式。

UITableViewCell *cell = textView.superview.superview;
// you will get warnings on that.  because you're calling superview, it expects it to be
// a UIView instance, but if you call NSLog(@"%@", textView.superview.superview), it will
// return UITableViewCell, so you can ignore the warning.  anyway, assuming you have a 
// reference to your tableView, you find the index path with the next line
NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
// then call the didSelect method
[self tableView:myTableView didSelectRowAtIndexPath:indexPath];

If you embed your textView in something other than the contentView, this won't work, you'll just have to add or subtract superview calls in an NSLog statement until you find the cell.如果您将 textView 嵌入到 contentView 之外,这将不起作用,您只需在 NSLog 语句中添加或减去 superview 调用,直到找到该单元格。 But that should do it for you但这应该为你做

Attach both tap and swipe recognizers to the text view programmatically.以编程方式将点击和滑动识别器附加到文本视图。 Set the state of the swipe recognizer to disabled at start.将滑动识别器的 state 设置为在启动时禁用。 On tap, enable the swipe recognizer.点击时,启用滑动识别器。 If taps are to enable/disable the swipe, that should be easy to do as well.如果点击要启用/禁用滑动,那也应该很容易做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM