【轉】iOS學習之iOS禁止Touch事件

iOS程序中有時會有需要禁止應用接收Touch的要求(比如動畫進行時,防止觸摸事件觸發新方法)。

 一、一般有兩種:

  1、弄個遮罩層,禁止交互;

  2、使用UIApplication中的方法進行相關的交互設置,方法如下:

// 開始禁止交互
- (void)beginIgnoringInteractionEvents NS_EXTENSION_UNAVAILABLE_IOS("");               // nested. set should be set during animations & transitions to ignore touch and other events
// 結束禁止交互
- (void)endIgnoringInteractionEvents NS_EXTENSION_UNAVAILABLE_IOS("");
// 是否禁止了交互
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, getter=isIgnoringInteractionEvents) BOOL ignoringInteractionEvents;                  // returns YES if we are at least one deep in ignoring events
#else
- (BOOL)isIgnoringInteractionEvents;                  // returns YES if we are at least one deep in ignoring events
#endif

 二、具體在代碼裏的實現如下:

  1、禁止交互

if (![[UIApplication sharedApplication] isIgnoringInteractionEvents])    //禁止交互
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

  2、由禁止變爲啓用交互

if ([[UIApplication sharedApplication] isIgnoringInteractionEvents])
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];   //啓用交互

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章