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

查看:523
本文介绍了在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. 设置混合模式并在其上绘制

  1. create an image context
  2. capture the views under your view using -[CALayer renderInContext:] for each
  3. create an image from the image context
  4. draw that image into your view
  5. set the blend mode and draw on top of that

但这将是缓慢而脆弱的,并且不会如果您为任何视图设置动画,则可以正常工作我不推荐它。

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天全站免登陆