在 iOS 中使用混合模式堆叠 UIViews [英] Stacking UIViews with blend modes in iOS

查看:28
本文介绍了在 iOS 中使用混合模式堆叠 UIViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的UIImageViews.我想让顶部 UIImageView 使用屏幕混合模式与底部 UIImageView 混合.

I have two different UIImageViews. I'd like to make the top UIImageView blend in using the Screen blend mode with the bottom UIImageView.

我知道 CALayer 的属性:compositingFilter 并且我知道它在 iOS 中不起作用.我已经搜索了很多解决方案,我发现应该如何子类化 UIView 并覆盖 drawRect.

I know of the property of CALayer: compositingFilter and I know that it doesn't work in iOS. I've searched a lot for solutions, and I've found how one should subclass UIView and override drawRect.

我已经尝试将 drawRect 中的上下文设置为屏幕混合模式,尽管它仍然可以正常绘制每一幅图像.也许我做错了什么,或者方法应该不同.也许我需要 OpenGL 或 CALayer 来实现这一点.有人可以帮忙吗?

I've tried to set the context in drawRect to the screen blend mode, although it still draws every one of the images normally. Perhaps I am doing something wrong, or the approach should be different. Maybe I need OpenGL or CALayer to achieve this. Could someone assist?

推荐答案

遗憾的是,无法在 iOS 上的 UIViews 之间进行非复合混合.UIKit 不提供该功能,您已经注意到,CALayer 也无法提供.

Unfortunately there is no way to do a non-composite blend between UIViews on iOS. UIKit doesn't provide the functionality, and you've already noted, CALayer can't do it either.

一般来说,在 UIView 中实现 -drawRect 对你没有帮助.您正在绘制一个空位图 - 它不包含其背后的视图位,因为这些位可能随时更改(任何视图或图层都可能被动画化).CA 从根本上假设层的内容应该相互独立.

In general, implementing -drawRect in a UIView won't help you. You're drawing into an empty bitmap -- it doesn't contain the bits of the views behind it, since those might change at any time (any view or layer might be animated). CA fundamentally assumes that layers' contents should be independent of each other.

可以尝试,在你的-drawRect:

  1. 创建图像上下文
  2. 使用 -[CALayer renderInContext:] 为每个视图捕获视图
  3. 从图像上下文创建图像
  4. 将该图像绘制到您的视图中
  5. 设置混合模式并在其上绘制

但这会很慢而且很脆弱,如果你为任何视图设置动画就行不通.我不会推荐它.

But that will be slow and fragile, and won't work if you animate any of the views. I wouldn't recommend it.

如果您真的需要这样做,您将不得不将整个场景切换为使用 OpenGL 进行渲染,你有更多的自由.

If you really need to do this, you're going to have to switch your whole scene to render with OpenGL, where you've got more freedom.

这篇关于在 iOS 中使用混合模式堆叠 UIViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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