What Happens When I Touch the Screen
来源:互联网 发布:pp助手不能安装软件 编辑:程序博客网 时间:2024/05/22 20:38
from:http://www.twistedape.me.uk/blog/2013/10/05/ios-what-happens-when-i-touch-the-screen/
iOS: What Happens When I Touch the Screen?
When you touch the screen in iOS the first thing that needs to happen is to find the correct object to handle the tap or gesture. The way this is done is by calling
on the key window of the application (UIWindow is a subclass of UIView). This method will return the view that should handle gesture.
If you have set userInteractionEnabled
to NO
on this view then hitTest:withEvent:
will always return nil
. This will mean that neither the view nor any of its subviews will respond to touches. If user interaction is enabled then hitTest:withEvent:
will check to see if it contains the point p
. If it doesn’t contain the point p
then it will return NO
. This means means that none of the view’s subviews will be tested to see if they contain the point. This will cause a problem if you expect a view that goes over the edge of its superview’s bounds to respond to touches (it will only respond to touches that are also inside the superview’s bounds).
If hitTest:withEvent:
determines that the point is within the view’s bounds then it will start calling hitTest:withEvent:
on its subviews. If a subview returns a UIView
then this is the UIView
that the current hitTest:withEvent:
will also return (it won’t check the other subviews). This results in the UIView
that it returned being the one that handles the event. If none of the subviews returns a UIView
then hitTest:withEvent:
returns self
, and the current view will handle the event.
The order that hitTest:withEvent:
checks the subviews is important, as if there are two subviews that might both respond to a touch then only the first subview to havehitTest:withEvent:
called on it will respond to that touch. The order that the subviews are called is the reverse of the order that you add them to the view i.e. the last subview added to the view is the first subview that will have hitTest:withEvent:
called on it.
So hitTest:WithEvent:
looks something like this:
This is useful stuff to know, as sometimes there may be a situation where something is not responding, and you’d like it to. For instance, if I have some views as in this image:
Here the button is a subview of the red view. In this case if you clicked on the right hand side of the button (above the white background) it wouldn’t respond. To solve this you can either make it so pointInside:withEvent
(on the red view) returns YES
for the right-hand part of the button (if you do this the red view will handle the touch if the button doesn’t). Or you can make it so hitTest:withEvent:
(on the red view) returns the button when the point is over the button, but not over the red view.
So you’d need to subclass the UIView
for the red view. If you override the pointInside:withEven:
it might look something like this:
If instead you override the hitTest:withEvent:
method then it may look something like this:
This will work well if the button is only over superviews. However if the button is over a sibling view, or some other path in the view hierarchy the other path in the view hierarchy may be hit tested first and return a UIView
; so the button’s superview doesn’t get hit tested, and the button won’t respond. In that case you may need to either change the order of some subviews (if you can), or override some other hit testing methods so the siblings don’t respond for the points under the button.
- What Happens When I Touch the Screen
- What happens when the Fed starts losing money
- What happens when I disable SSID Broadcast? Am I more secure?
- What happens when media & big data collide?
- What happens when you run a container?
- Q: When I try to animate the frame of a CALayer nothing happens. Why?
- What happens when there is a cache miss?
- What really happens when you navigate to a URL
- What really happens when you navigate to a URL
- What really happens when you navigate to a URL
- What really happens when you navigate to a URL
- what happens when you type in a URL in browser
- What really happens when you navigate to a URL
- What really happens when you navigate to a URL
- What happens when you type a URL in browser
- what happens when you type a url in browser
- What really happens when you navigate to a URL
- What really happens when you navigate to a URL
- TCP状态分析方法
- Winform窗体之间的传值
- 微信、陌陌的架构方案分析(LBS之二)
- sublime test 3 工具使用
- cocos2dx 点击事件分析(5)
- What Happens When I Touch the Screen
- 关于缓存算法
- 蓝桥杯——矩阵翻硬币
- VS检测内存泄漏
- ubuntu server 14.04 安装图形界面
- 对某个Android应用的某一功能测试
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- C学习笔记
- OGNL表达式struts2标签“%,#,$”