CAShapeLayer 用户交互缓慢

发布于 2024-10-17 17:11:21 字数 706 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-10-24 17:11:21

请记住,当您在图层上设置位置时(假设它不是默认禁用操作的 UIView 的根图层),它会隐式动画到新位置,这需要 0.25 秒。如果您想让它变得更快,请暂时禁用图层上的操作,如下所示:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
  CGPoint activePoint = [[touches anyObject] locationInView:self];
  CGPoint newPoint = CGPointMake(activePoint.x - 
                              currentPoint.x,activePoint.y - currentPoint.y);

  [CATransaction begin];
  [CATransaction setDisableActions:YES];
  curLayer.position = CGPointMake(shapeLayer.position.x + 
                             newPoint.x, shapeLayer.position.y + newPoint.y);
  [CATransaction commit];
  currentPoint = activePoint;
}

这应该会导致它跳到新位置而不是动画。如果这没有帮助,那么让我看一下您的图层初始化代码,以便我可以了解它具有哪些属性和尺寸。例如,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:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
  CGPoint activePoint = [[touches anyObject] locationInView:self];
  CGPoint newPoint = CGPointMake(activePoint.x - 
                              currentPoint.x,activePoint.y - currentPoint.y);

  [CATransaction begin];
  [CATransaction setDisableActions:YES];
  curLayer.position = CGPointMake(shapeLayer.position.x + 
                             newPoint.x, shapeLayer.position.y + newPoint.y);
  [CATransaction commit];
  currentPoint = activePoint;
}

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.

離殇 2024-10-24 17:11:21

尝试设置 shouldRasterizeYES,特别是当它通常以相同比例绘制时。如果您的应用程序在高 DPI 设备上运行,您可能还需要设置 rasterizationScale 以匹配图层的 contentsScale

虽然对形状进行栅格化可以使形状移动速度更快,但在对图层的路径或大小进行动画处理时,您可能需要暂时禁用栅格化。

Try setting shouldRasterize to YES on your CAShapeLayer, particularly if it is usually drawn at the same scale. If your app runs on high-DPI devices, you may also need to set rasterizationScale to match the layer’s contentsScale.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文