ios - 如何为 iOS 应用程序正确使用 setNeedsDisplayInRect?

标签 ios swift xcode7

我在 Yosemite 10.10.5 和 Xcode 7 上,使用 Swift 制作一款针对 iOS 8 及更高版本的游戏。

编辑:更多可能有用的细节:这是一个 2D 益智/街机游戏,玩家可以移动石头来匹配它们。根本没有3D渲染。绘图已经太慢了,我什至还没有用碎片爆炸。还有一个级别淡入,非常令人担忧。但到目前为止,这都是在模拟器上进行的。我还没有实际的 iPhone 来测试,我打赌实际的设备至少会快一点。

我有自己的 Draw2D 类,它是一种 UIView,设置为 this tutorial .我有一个 NSTimer,它在 Draw2D 中启动以下调用链:

[setNeedsDisplay];//其中调用了 drawRect,它是 Draw2D 的主要绘制函数

drawRect(rect: CGRect)
{
  scr_step(); // the master update function, which loops thru all objects and calls their individual update functions. I put it here so that updating and drawing are always in sync

  CNT = UIGraphicsGetCurrentContext(); // get the curret drawing context

  switch (Realm) // based on what realm im in, call the draw function for that realm
  {
    case rlm.intro: scr_draw_intro();
    case rlm.mm: scr_draw_mm();
    case rlm.level: scr_draw_level(); // this in particular loops thru all objects and calls their individual draw functions

    default: return;
  }

  var i = AARR.count - 1; // loop thru my own animation objects and draw them too, note it's iterating backwards because sometimes they destroy themselves
  while (i >= 0)
  {
    let A = AARR[i];
    A.scr_draw();

    i -= 1;
  }
}

所有绘图都可以正常工作,但速度很慢。

现在的问题是我想优化绘图。我只想在需要绘制的脏矩形中绘制,而不是整个屏幕,这是 setNeedsDisplay 正在做的事情。

我找不到任何教程或好的示例代码。我找到的最接近的是苹果的文档 here ,但除其他事项外,它没有解释到目前为止如何获得所有脏矩形的列表。它也没有明确说明是否在每次调用 drawRect 结束时自动清除脏矩形列表?

它也没有解释我是否必须根据矩形手动裁剪所有绘图。我在网络上发现了相互矛盾的信息,显然不同的 iOS 版本会有所不同。特别是,如果我要手动剪辑东西,那么我首先看不到苹果核心功能的意义。我可以只维护自己的矩形列表,然后手动将每个绘图目标矩形与脏矩形进行比较,看看我是否应该绘制任何东西。然而,这将是一个巨大的痛苦,因为我在每个关卡中都有一张背景图片,我不得不在每个移动的物体后面绘制一张背景图片。 我真正希望的是使用 setNeedsDisplayInRect 的正确方法 让核心框架对下一个绘制周期绘制的所有内容进行自动裁剪,以便它自动仅绘制那部分背景加上顶部的移动物体。

所以我尝试了一些实验:首先在我的石头阵列中:

func scr_draw_stone()
{
  // the following 3 lines are new, I added them to try to draw in only dirty rectangles
  if (xvp != xv || yvp != yv) // if the stone's coordinates have changed from its previous coordinates
  {
    MyD.setNeedsDisplayInRect(CGRectMake(x, y, MyD.swc, MyD.shc)); // MyD.swc is Draw2D's current square width in points, maintained to softcode things for different screen sizes.
  }

  MyD.img_stone?.drawInRect(CGRectMake(x, y, MyD.swc, MyD.shc)); // draw the plain stone
  img?.drawInRect(CGRectMake(x, y, MyD.swc, MyD.shc)); // draw the stone's icon
}

这似乎并没有改变什么。事情进展得和以前一样慢。那么我把它放在括号中:

[MyD.setNeedsDisplayInRect(CGRectMake(x, y, MyD.swc, MyD.shc))];

我不知道括号的作用,但我原来的 setNeedsDisplay 是在括号中,就像他们在教程中所说的那样。所以我在我的石头对象上试了一下,但也没有效果。

那么我需要做什么才能使 setNeedsDisplayInRect 正常工作?

现在,我怀疑我的主绘图函数需要一些条件检查,例如:

if (ListOfDirtyRectangles.count == 0)
{
  [setNeedsDisplay]; // just redraw the whole view
}
else
{
  [setNeedsDisplayInRect(ListOfDirtyRecangles)];
}

但是我不知道内置脏矩形列表的名称。我找到了 this说方法名称是 getRectsBeingDrawn,但这是针对 Mac OSX 的。它在 iOS 中不存在。

谁能帮帮我?我在正确的轨道上吗?我对 Mac 和 iOS 还是比较陌生。

最佳答案

如果可能的话,你真的应该避免重写drawRect。现有的 View /技术利用任何硬件功能使事情比在图形上下文中手动绘制快得多,包括缓冲 View 的内容、使用 GPU 等。这在“View Programming Guide for iOS”。

如果您有背景和其他对象,您可能应该为它们使用单​​独的 View 或图层,而不是重新绘制它们。

你也可以考虑SpriteKit、SceneKit、OpenGL ES等技术

除此之外,我不太确定我是否理解您的问题。当您调用 setNeedsDisplayInRect 时,它会将该矩形添加到需要重新绘制的矩形中(可能与列表中已有的矩形合并)。 drawRect: 稍后将被调用以一次绘制这些矩形。

setNeedsDisplayInRect/drawRect: 分离的重点是确保重绘给定 View 部分的多个请求合并在一起,并且只绘制一次每个重绘周期。

你不应该在 drawRect: 中调用你的 scr_step 方法,因为它可能在循环重绘周期中被调用多次。这在“查看 iOS 编程指南”(强调我的)中有明确说明:

The implementation of your drawRect: method should do exactly one thing: draw your content. This method is not the place to be updating your application’s data structures or performing any tasks not related to drawing. It should configure the drawing environment, draw your content, and exit as quickly as possible. And if your drawRect: method might be called frequently, you should do everything you can to optimize your drawing code and draw as little as possible each time the method is called.

关于裁剪,drawRect 的文档指出:

You should limit any drawing to the rectangle specified in the rect parameter. In addition, if the opaque property of your view is set to YES, your drawRect: method must totally fill the specified rectangle with opaque content.

不知道您的 View 显示什么,您调用的各种方法做什么,实际需要时间的是什么,很难提供更多关于您可以做什么的洞察力。提供有关您实际需求的更多详细信息,我们可能会提供帮助。

关于ios - 如何为 iOS 应用程序正确使用 setNeedsDisplayInRect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34716074/

相关文章:

ios - iOS 中的 FCM 集成问题

iphone - 如何将 DetailViewController 的导航项上的后退按钮设置为 "Back"而不是上一个表格的标题?

xcode - Facebook 登录问题 -canOpenURL : failed for URL: "fbauth2:///" - error: "(null)"

ios - swift/iOS : Data takes a few seconds to load when screen appears

使用断点调试时 Xcode 7.3 崩溃

xcode - In Xcode 7 playground is not showing is markup format 的评论

ios - 如何在 iOS 5 中使用 HTML 标签发送图像而不是作为附件

ios - 重建应用程序时文档目录路径更改

swift - 无法在 Swift 中创建符合协议(protocol)的类型数组

ios - 在静态 UITableView 上使用 UITextfields 的上一个和下一个键盘按钮