响应touchesBegan在UIPickerView而不是UIView [英] Responding to touchesBegan in UIPickerView instead of UIView

查看:273
本文介绍了响应touchesBegan在UIPickerView而不是UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIPickerView,当不使用时,淡出到20%的alpha。我想让用户能够触摸选择器并让它退色。



我可以让它工作,如果我把一个touchesBegan方法在主视图,但这只有当用户触摸视图时才起作用。我尝试了子类UIPickerView和有一个touchesBegan在那里,但它没有工作。



我猜这是与响应链有关,但可以

解决方案

我一直在寻找这个问题的解决方案一个多星期。我回答你,即使你的问题是一岁以上,希望这可以帮助别人。



对不起,如果我的语言不是很技术,但我漂亮新的Objective-C和iPhone开发。



子类化UIpickerView是正确的方法。但是你必须重写 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 方法。这是每次触摸屏幕时调用的方法,它返回将对触摸做出反应的视图。换句话说,将调用 touchesBegan:withEvent:方法的视图。



UIPickerView有9个子视图!在UIPickerView类中实现 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 不会返回 self (这意味着 touchesBegan:withEvent:你写在子类中不会被调用),但会返回一个子视图,诀窍是让 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *);这是一个非法的子类(称为UIPickerTable)。



< event
方法返回 self ,以便您可以控制 touchesBegan:withEvent: code> touchesMoved:withEvent:和 touchesEnded:withEvent:



在这些方法中,为了保持UIPickerView的标准功能,您必须记住在UIPickerTable子视图中再次调用它们。



我希望这有意义。我现在不能写代码,只要我在家,我将编辑这个答案,并添加一些代码。


I have a UIPickerView that gets faded out to 20% alpha when not in use. I want the user to be able to touch the picker and have it fade back in.

I can get it to work if I put a touchesBegan method on the main View, but this only works when the user touches the View. I tried sub-classing UIPickerView and having a touchesBegan in there, but it didn't work.

I'm guessing it's something to do with the Responder chain, but can't seem to work it out.

解决方案

I've been searching for a solution to this problem for over a week. I'm answering you even if you're question is over a year old hoping this helps others.

Sorry if my language is not very technical, but I'm pretty new to Objective-C and iPhone development.

Subclassing UIpickerView is the right way to do it. But you've to override the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method. This is the method called whenever you touch the screen and it returns the view that will react to the touch. In other words the view whose touchesBegan:withEvent: method will be called.

The UIPickerView has 9 subviews! In the UIPickerView class implementation - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event won't return self (this means the touchesBegan:withEvent: you write in the subclass won't be called) but will return a subview, exactly the view at index 4 (an undocumented subclass called UIPickerTable).

The trick is to make the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method to return self so you have control over the touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent: methods.

In these methods, in order to keep the standard functionalities of the UIPickerView, you MUST remember to call them again but on the UIPickerTable subview.

I hope this makes sense. I can't write code now, as soon as I'm at home I will edit this answer and add some code.

这篇关于响应touchesBegan在UIPickerView而不是UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆