简体   繁体   English

在UIView / UIWindow上应用过滤器以修改基础视图呈现

[英]Apply filter over UIView/UIWindow to modify underlying view rendering

I'm trying to make a "dark mode" for my app, and I'd like to do it in a really easy way. 我正试图为我的应用程序制作一个“黑暗模式”,我想以一种非常简单的方式做到这一点。 Is there any way to apply a filter or create another view over my other views so that they appear inverted - much like "Accessibility" on iOS 3.2+? 有没有办法应用过滤器或在我的其他视图上创建另一个视图,使它们看起来倒置 - 就像iOS 3.2+上的“辅助功能”一样? I know that Core Image filters don't work on iPhone, but that's not a big deal to me - as long as there's a way to apply a different sort of filter. 我知道Core Image过滤器在iPhone上不起作用,但这对我来说并不是什么大问题 - 只要有一种方法可以应用不同类型的过滤器。

Is this feasible on iPhone? 这在iPhone上可行吗? How does Apple do it? Apple如何做到这一点?

It sounds like you're thinking of placing a semi-transparent view filled with a dark color over your entire user interface. 听起来您正在考虑在整个用户界面上放置一个充满深色的半透明视图。 If that's the case, then you should read Forwarding Touch Events for a discussion of the issues involved. 如果是这种情况,那么您应该阅读转发触摸事件以讨论所涉及的问题。

Another way to do it is to change the set of colors and images that you use to draw your views. 另一种方法是更改​​用于绘制视图的颜色和图像集。

Both these schemes will be easier and work better if you're dealing with custom views. 如果您处理自定义视图,这两种方案都会更容易并且工作得更好。 If you're using an overlay, the document linked above explains that the standard UI elements don't take kindly to event forwarding. 如果您使用的是叠加层,则上面链接的文档会说明标准UI元素并不适用于事件转发。 If you change colors, well... depending on what UI elements you're using, it may or may not be possible to substitute your own images and colors. 如果你改变颜色,那么...取决于你正在使用的UI元素,可能会或可能不会替换你自己的图像和颜色。

Try this! 试试这个!

First, create a layer! 首先,创建一个图层!

let foreground = CALayer() 
foreground.backgroundColor = UIColor.blue.cgColor //Whatever color you want
foreground.opacity = 0.5
foreground.frame = (UIApplication.shared.keyWindow?.bounds)
foreground.masksToBounds = true

Then just add the layer to your root view controller 然后只需将图层添加到根视图控制器

UIApplication.shared.keyWindow?.rootViewController?.view.layer.addSublayer(foreground)

Hope this can help someone else! 希望这可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM