UIControlState“应用程序"有什么用?UIButton? [英] Whats the use of UIControlState "application" of UIButton?

查看:29
本文介绍了UIControlState“应用程序"有什么用?UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也浏览了 apple doc,但它只是指出它的

I went through the apple doc too but it just states that its

可供应用程序使用的其他控制状态标志.

Additional control-state flags available for application use.

它只是一个getter方法,那么什么时候设置?

Its just a getter method so when does it get set?

推荐答案

applicationreserved 基本上是标记.当查看它们的 Objective-c 文档时,这一点会更清楚:

application and reserved are basically markers. That is more clear when looking at the objective-c documentation for them:

禁用:UIControlStateDisabled = 1 <<1

应用程序:UIControlStateApplication = 0x00FF0000

保留:UIControlStateReserved = 0xFF000000

这意味着 UIControlState 的第二个最低有效位例如负责确定 UIControl 是否被禁用.17 - 24(从 1 <<161 <<23)的所有位都可供您的应用程序使用使用 25 - 32(从 1 <<241 <<31)供内部框架使用.

That means that the second least significant bit of a UIControlState for example is responsible for determining wether or not a UIControl is disabled or not. All the bits from 17 - 24 (from 1 << 16 until 1 << 23) are there for your application to use while 25 - 32 (from 1 << 24 until 1 << 31) are there for internal frameworks to use.

这基本上意味着 Apple 能够/允许在使用最低 16 位时定义控件的新状态标志,您可以保证能够使用 8 位作为您自己的自定义标志.

That basically means that Apple is able / allowed to define new state flags of controls while using the lowest 16 bits, you have the guarantee to be able to use 8 bits for custom flags of your own.

可以定义自定义标志,例如通过:

Defining custom flags can be done e.g. via:

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) 

这篇关于UIControlState“应用程序"有什么用?UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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