更改 UITextField 的默认触摸事件
好吧,这就是我想做的。我有一个 UITextField。当我单击它时,我想调用我的方法之一。当我双击(用 1 根手指点击两次)它时,我想编辑文本字段(就像我在普通的 UITextField 上单击它一样。)
我不知道如何处理这个问题。我正在考虑对 UITextField 进行子类化并尝试在发送时拦截触摸事件,但我无法弄清楚要重写哪些方法来执行此操作。我应该重写哪些方法以及如何重写?
或者,如果有更好的方法来做到这一点,请告诉我!我洗耳恭听,不知道如何继续。
Alright, so here is what I am trying to do. I have a UITextField. When I single tap it, I want to call one of my methods. When I double tap (tap it twice with 1 finger) it, I want to edit the text field (as though I had single tapped it on a normal UITextField.)
I am not sure how to go about this. I was thinking of sub-classing UITextField and trying to intercept the touch event as it is sent, but I can't figure out which methods to override to do this. Which methods should I override and how?
Or, if there is a better way to do this, let me know! I'm all ears, and not sure how to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将涉及几个步骤:
1.) 添加一个 NSBoolean 属性,用于跟踪用户最近是否已经点击过该字段一次(您可以决定这里的“最近”意味着什么)。
2.) 实现分配给 UITextField 的委托的 textFieldShouldBeginEditing: 方法。如果用户快速连续点击两次(可通过检查布尔属性是否为 true 来检测),则返回 YES。如果没有,则调用您的方法,然后返回 NO。
This would involve several steps:
1.) Add a NSBoolean property that keeps track of whether the user has already tapped the field once recently (you get to decide what recently means here).
2.) Implement the textFieldShouldBeginEditing: method of the delegate assigned to your UITextField. If the user has tapped twice in quick succession (detectable by checking whether or not the boolean property is true), then return YES. If not, call your method, and then return NO.