使用swift,是否可以访问可访问性中的反转颜色功能?

问题描述:

苹果已经在手机中加入的功能是一般>辅助功能>反转颜色,我可以在我的程序中以某种方式使用它,例如当用户触摸屏幕时颜色反转吗?

The function that apple has already put in the phone that is in general>accessibility>invert colors, can I somehow use that in my program so for say when the user touches the screen the colors invert?

我不知道有什么方法可以自动执行此操作,但是您可以使用 UIColor 上的扩展并访问子视图来自己反转颜色?

I don't know of a way to do this automatically, but you could invert colors yourself using an extension on UIColor and accessing the subviews?

extension UIColor {
    var inverted: UIColor {
        var r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0, a: CGFloat = 0.0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)
        return UIColor(red: (1 - r), green: (1 - g), blue: (1 - b), alpha: a) // Assuming you want the same alpha value.
    }
}

然后如果你想更新视图的特定属性,你可以这样做:

And then if you want to update specific properties of the views you could do something like this:

  view.subviews.map { $0.backgroundColor = $0.backgroundColor.invertedColor }
  // And so on to change things like the tint color, text color, etc.

抱歉,我不知道有什么方法可以直接做到这一点,但在那之前,我猜总比没有好.

Sorry, I don't know a way to do this directly but till then this is better than nothing I guess.