userInteractionEnabled与enabled的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/85231296

首先.userInteractionEnabled是UIView类中的一个属性.
enabled是UIControl类中的一个属性.
UIControl继承与UIView.
在响应者链中有这么一段描述

UIKit uses view-based hit-testing to determine where touch events occur. Specifically, UIKit compares the touch location to the bounds of view objects in the view hierarchy. The hitTest:withEvent: method of UIView traverses the view hierarchy, looking for the deepest subview that contains the specified touch, which becomes the first responder for the touch event.

就是说通过hitTest:withEvent:这个方法来寻找响应的视图.hitTest:withEvent:方法中有这么一段记载

This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.

意思是,如果视图隐藏或者userinteractions(用户交互,也就是userInteractionEnabled属性).或者alpha < 0.01.hitTest会自动忽略(往下继续传递).

所以说.如果设置userInteractionEnabled为NO.点击方法或者是触摸事件之类的会穿透视图.
而设置enabled为NO的话会禁止该控件的交互,并且.事件不会穿透.

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/85231296