使用swift,是否可以访问可访问性中的反转颜色功能? [英] With swift, is it possible to access the invert colors function that’s in accessibility?

查看:21
本文介绍了使用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.

这篇关于使用swift,是否可以访问可访问性中的反转颜色功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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