CAShapeLayer 用户交互缓慢
我有一个 CAShapeLayer,它必须执行一项在用户手指引导下在屏幕上移动的简单任务。
问题是动作太慢了。该图层确实在移动,但存在滞后并且感觉很慢。
我有另一个测试应用程序,其中 UIImage 被移动,并且完全没有延迟,并且图像立即移动。
我可以做什么来克服这个问题?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { currentPoint = [[touches anyObject] locationInView:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint activePoint = [[touches anyObject] locationInView:self]; CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y); curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y); currentPoint = activePoint; }
谢谢!
I have a CAShapeLayer and it has to do a simple task of moving on the screen, guided by the user's finger.
The problem is that the movement is too slow. The layer does move, but there is a lag and it feels slow.
I have another test app where an UIImage is moved and there is no lag at all and the image moves instantly.
What can I do to overcome this?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { currentPoint = [[touches anyObject] locationInView:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint activePoint = [[touches anyObject] locationInView:self]; CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y); curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y); currentPoint = activePoint; }
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,当您在图层上设置位置时(假设它不是默认禁用操作的 UIView 的根图层),它会隐式动画到新位置,这需要 0.25 秒。如果您想让它变得更快,请暂时禁用图层上的操作,如下所示:
这应该会导致它跳到新位置而不是动画。如果这没有帮助,那么让我看一下您的图层初始化代码,以便我可以了解它具有哪些属性和尺寸。例如,cornerRadius 等属性可能会影响性能。
Keep in mind that when you set the position on a layer (assuming it's not the root layer of a UIView on which actions are disabled by default), it implicitly animates to the new position, which takes 0.25 seconds. If you want to make it snappier, temporarily disable actions on the layer like this:
This should cause it to jump to the new position rather than animate. If that doesn't help, then let me take a look at your layer init code so I can see what properties and dimensions it has. Properties such as cornerRadius, for example, can affect performance.
尝试设置
shouldRasterize
为YES
,特别是当它通常以相同比例绘制时。如果您的应用程序在高 DPI 设备上运行,您可能还需要设置rasterizationScale
以匹配图层的contentsScale
。虽然对形状进行栅格化可以使形状移动速度更快,但在对图层的路径或大小进行动画处理时,您可能需要暂时禁用栅格化。
Try setting
shouldRasterize
toYES
on yourCAShapeLayer
, particularly if it is usually drawn at the same scale. If your app runs on high-DPI devices, you may also need to setrasterizationScale
to match the layer’scontentsScale
.While rasterizing your shape can make it faster to move the shape around, you’ll probably want to temporarily disable rasterization while you’re animating the layer’s path or size.