简体   繁体   English

我如何强制“容器” UIView没有CALayer?

[英]How I force a “container” UIView to have no CALayer?

I am trying to conserve memory consumption in my visualization about. 我试图在我的可视化中节省内存消耗。 I have a scrollView with a containerView that contains some child views. 我有一个带有包含一些子视图的containerView的scrollView。 I would like to eliminate the CALayer of the scrollView and containerView. 我想消除scrollView和containerView的CALayer。 So, just the child views consume memory during redraws, etc. 因此,仅子视图会在重绘等过程中占用内存。

How do I do this? 我该怎么做呢?

Thanks, Doug 谢谢,道格

All UIViews and UIView subclasses on the iPhone are backed by CALayers. iPhone上的所有UIViews和UIView子类均由CALayers支持。 There isn't really much you can do to avoid this because it's an assumption made by UIKit. 您实际上可以做很多事情来避免这种情况,因为这是UIKit所做的假设。 (In Cocoa for Mac, you can specify which views have CALayers, though!) (不过,在Mac的Cocoa中,您可以指定哪些视图具有CALayers!)

While you might be able to hack a few things and destroy the CALayers of certain views, I wouldn't recommend it. 尽管您可能可以破解一些东西并销毁某些视图的CALayers,但我不建议这样做。 I've written some pretty complex painting apps, and I've never had a problem with CALayers consuming large amounts of memory. 我已经编写了一些非常复杂的绘画应用程序,而CALayers消耗大量内存从来没有问题。 Remember, you can safely use about 25MB of memory before your app receives a memory warning. 请记住,在应用收到内存警告之前,您可以安全地使用大约25MB的内存。 If you're seeing your app churn through large amounts of memory used and you're just drawing, there's probably another problem. 如果您看到应用程序通过大量使用的内存而流失,而您只是在绘图,那么可能还有另一个问题。

Hope that helps, 希望能有所帮助,

Ben

My container layer is only 1x1 points large, and therefore uses very little memory. 我的容器层只有1x1点大,因此使用的内存很少。 (I haven't tried 0x0 and don't know if that'll work.) (我还没有尝试过0x0,也不知道那是否可行。)

With clipsToBounds == NO (the default), all the subviews are still visible, even though they're "sticking out" of their parent view. 使用clipsToBounds == NO (默认),即使所有子视图“伸出”其父视图,它们仍然可见。

If your subviews need to receive events you'll need to overwrite -pointInside:withEvent: to return YES for points inside the "real" bounds of your container view. 如果子视图需要接收事件,则需要覆盖-pointInside:withEvent:以对容器视图的“真实”范围内的点返回YES

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

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