- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的问题是关于 iOS 开发的。我正在编写一个简单的游戏,以测试低级核心图形功能(上下文、图层等)以及如何将它与实际的简单 View 混合......我解释一下:我在屏幕上有一个简单的 sprite,它使用 UIImageView
属性进行动画处理,因此帧速率完美运行。之后,我添加了一个使用 bezier 曲线
动态生成的无限背景。为了制作动画,我使用了 CADisplayLink
,在背景上调用方法 “animate”
,在 bezier curve
上移动我的点,并且然后我调用 setNeedsDisplay
。它“或多或少”工作得很好,但是如果我删除背景(或 setNeedsDisplay
调用,所以它不会在每一帧都重新生成)帧速率会增加很多,所以“任何”都不是在这部分工作正常......
我也尝试过双缓冲系统,但它的效果比我目前的代码差。我也考虑过使用线程,但我真的没有发现 CPU 在后台做任何困难的事情。
代码如下:
//This is called automatically with CADDisplayLink.
- (void) animate:(CADisplayLink *)sender
{
//Check for deleting the first point.
float x = points[firstPoint].x;
if ( x < -2*DIST ) {
CGFloat lastX = points[lastPoint].x;
lastPoint = firstPoint;
firstPoint = ((firstPoint + 1) % (TOTAL_POINTS));
points[lastPoint] = CGPointMake(lastX + DIST, [self getNextY]);
}
//Move points
for(int i = 0; i < TOTAL_POINTS; i++) {
points[i].x -= 7;
}
//Mark as "needed to redraw"
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 0, 0, 1.0);
CGContextSetLineCap(context, kCGLineCapRound);
//Create path
CGMutablePathRef path = CGPathCreateMutable();
//Draw the curve
[self drawBezierCurveInPath:path];
//Close and add path
CGPathAddLineToPoint(path, NULL, 485, -5);
CGPathAddLineToPoint(path, NULL, -5, -5);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, bg2);
// [[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
//Release
CGPathRelease(path);
//Paint time.
playintIntervalView.text = [NSString stringWithFormat:@"%f", playingInterval];
}
- (void) drawBezierCurveInPath: (CGMutablePathRef) path
{
CGPoint fp = midPoint(points[firstPoint], points[(firstPoint+1)%TOTAL_POINTS]);
CGPathMoveToPoint(path, NULL, fp.x, fp.y);
int i = firstPoint;
for (int k = 0; k < TOTAL_POINTS-2; k++) {
previousPoint2 = points[i];
previousPoint1 = points[(i+1)%TOTAL_POINTS];
currentPoint = points[(i+2)%TOTAL_POINTS];
// calculate mid point
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGPathAddQuadCurveToPoint(path, nil, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
i = (i + 1) % TOTAL_POINTS;
}
}
我更愿意存储一个UIBezierPath
(这样我就可以进行一些碰撞测试)但它不允许你只删除一个点(当它离开屏幕左侧时),所以我也必须在每一帧都初始化它……我错过了什么吗?
最佳答案
这可能有点晚了,但在这种情况下使用 CADisplayLink 制作动画并不是一个好主意。您应该研究 CoreAnimation 以获得高效的路径动画。
这里的问题是您试图每秒重绘 60 次,而 drawRect 中的代码可能无法在您拥有的毫秒内完成。通过删除 setNeedsDisplay,您基本上是在告诉它不要更新。
关于iphone - 帧速率与 CADisplayLink 和 drawRect 妥协,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925959/
在 Objective-C 中,我们可以使用代理模式初始化 CADisplayLink 来打破强引用: WeakProxy *weakProxy = [WeakProxy weakProxyForOb
我正在尝试制作一个应用程序,我必须触摸在屏幕上移动的按钮。我正在使用 CADisplayLink 来实现此目的。我的代码中的问题是它创建了一个新按钮而不是使用特定的按钮: @IBOutlet var
我在网上和文档中都找不到任何直接的答案。如果我使用以下内容设置 CADisplayLink: let displayLink = CADisplayLink(target: self, select
我正在尝试使用CADisplayLink来实现游戏循环的恒定帧速率。当在模拟器中测试它时,我无法实现稳定的时间范围。我应该考虑采用不同的方法吗? 或者这只是因为模拟器无法正确模拟硬件? 根据 Appl
我一直徒劳地试图理解 CADisplayLink.timestamp 的浮点值是什么。以纳秒为单位吗?秒?它的精度是多少? 最佳答案 文档 here声明该属性属于 CFTimeInterval 类型,
我将 CADisplayLink 用作选通计时器。 我有 2 个 CADisplayLinks: 主要的(在整个过程中运行): SMPTELink = [CADisplayLink displayLi
这是我的第一篇文章。如有错误,请多多包涵。 我是 iPhone 开发新手。我正在使用 CADisplayLink 来执行我的游戏的“更新”循环。在 XCode 8.2 中,我使用以下代码来计算两个连续
我的应用程序有问题。我想要发生的是当我单击按钮 2 时,它消失并停止移动。现在发生的事情是,当我单击 button2 时,它会消失但不会停止移动(即使它是隐藏的)。有帮助吗?代码: @IBOutlet
对于我的游戏循环和某些动画,我想知道自上次调用 render: 方法以来耗时间隔。 为此,我使用 CADisplayLink 并获取当前时间戳并减去上次调用的时间戳。这应该给我两个帧/调用之间的正确时
我用 CADisplayLink 替换了 NSTimer。我让它工作正常,但运行速度太慢。我怎样才能加快速度?这是我正在使用的代码: 在接口(interface)中我声明了一个实例变量: CADisp
我注意到在使用 CADisplayLink 时,异常会被吞没: CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWith
什么时候使用iOS quartz核心框架的CADisplayLink。 NSTimer 和 CADisplayLink 是一样的吗?什么是正确使用 CADisplayLink?我们要同步ios uiv
尝试重现 this article 中描述的行为.逐行。一切正常,但最后一步...... 问题是:我在调用 UIView.animate 之前取消暂停 CADisplayLink 并期望取消暂停的 C
我在我的应用程序中使用 CADisplayLink 作为计时器。我正在使用 CADisplayLink,因为我的应用程序非常依赖 CADisplayLink 的准确度/精度。就我而言,NSTimer
我正在制作游戏,我想在不同的线程上运行游戏逻辑代码和渲染代码,以期提高性能。但对于我来说,我无法弄清楚如何在生成的线程中运行 CADisplayLink。现在我正在使用 NSThread 进行多线程处
好吧,几个小时以来,我一直被这个编译器错误完全难住,问题是我所做的所有谷歌搜索都说它应该按照我的方式工作!我正在学习一本关于 iPhone 游戏开发的书籍教程,但由于随机编译错误,我卡在了第二章。 注
我有一个 CADisplayLink 触发 Director 对象中的 draw 方法。我想使 CADisplayLink 无效,然后解除分配一些由 Director 对象使用的单例缓存对象。 dra
我发现了 CADisplayLink 的一个大问题。 我有最基本的 EAGLLayer 和 OpenGL ES 1.1 绘制一个旋转三角形用于测试。这需要一个以屏幕刷新率调用的运行循环方法,所以我这样
我在我的 iOS 应用程序中使用了 NSTimer,但由于 SetNeedsDisplay,我没有得到我想要的结果。 我做了一些研究并找到了 CADisplayLink,它为我提供了我想要的动画结果。
如上一篇文章 ( Here ) 中所述,我正在创建一个从初始角度开始并移动到结束角度的动画。我决定使用 CADisplayLink 来实现此动画,因为动画需要在用户输入期间尽可能快地运行,因此使用 C
我是一名优秀的程序员,十分优秀!