2017-05-03 32 views
6

我通過apple doc也去了,但它只是說,它的什麼是UIButton的UIControlState「應用程序」的使用?

其他控件狀態可用於應用程序使用的標誌。

它只是一個getter方法,所以什麼時候它被設置?

+0

使用我認爲你的問題的身體+標題是有點誤導,因爲'UIControlState.application'是根本就沒有指定爲'UIButton',它也不是一種方法。你在問'UIControlState.application'的用法是什麼,以及任何UI元素的'state'屬性在什麼時候假定'.application'的值? – luk2302

+0

看到這個:https://developer.apple.com/reference/uikit/uicontrolstate – KKRocks

+0

@KKRocks我認爲OP看到,因爲引用的句子已經完全一樣。 – luk2302

回答

2

applicationreserved基本上是標記。

disabledUIControlStateDisabled = 1 << 1

applicationUIControlStateApplication = 0x00FF0000

reserved:那看着他們Objective-C的文檔時更加清晰UIControlStateReserved = 0xFF000000

這意味着,的第二個最低顯著位例如,UIControlState負責確定是否禁用UIControl。從17 - 24(從1 << 16直到1 << 23)的所有位都在供您的應用程序使用,而25 - 32(從1 << 24直到1 << 31)有內部框架可供使用。

這基本上意味着Apple可以/允許在使用最低16位時定義控件的新狀態標誌,您可以保證能夠使用8位自定義標誌。

可以完成定義自定義標記,例如,通過:

let myFlag = UIControlState(rawValue: 1 << 18) 

class MyButton : UIButton { 
    var customFlags = myFlag 
    override var state: UIControlState { 
     get { 
      return [super.state, customFlags] 
     } 
    } 

    func disableCustom() { 
     customFlags.remove(myFlag) 
    } 
} 

可以通過

let myButton = MyButton() 
print(myButton.state.rawValue) // 262144 (= 2^18) 
myButton.isEnabled = false 
myButton.isSelected = true 
print(myButton.state.rawValue) // 262150 (= 262144 + 4 + 2) 
myButton.disableCustom() 
print(myButton.state.rawValue) // 6 (= 4 + 2) 
+0

你的覆蓋var看起來像它返回一個UIControlState數組,但它返回一個UIControlState,狀態和customFlags異或。這怎麼樣? – Brynjar

+1

@Brynjar https://oleb.net/blog/2016/09/swift-option-sets/ - 它們符合['ExpressibleByArrayLiteral'](https://developer.apple.com/documentation/swift/expressiblebyarrayliteral) – luk2302

+0

@Brynjar並不像'XOR'那樣工作,它是一個按位或「或」或簡單的「+」,因爲在兩個不同的標誌中沒有一個位是「1」。 – luk2302

相關問題