CAShapeLayer缓慢的用户交互

ale*_*lex 4 core-animation quartz-graphics uikit cashapelayer cgpath

我有一个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;

}

谢谢!

Mat*_*ong 15

请记住,当您在图层上设置位置时(假设它不是默认情况下禁用操作的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;
}
Run Code Online (Sandbox Code Playgroud)

这应该使它跳转到新位置而不是动画.如果这没有帮助,那么让我看一下你的图层初始化代码,这样我就可以看到它有哪些属性和尺寸.例如,cornerRadius等属性可能会影响性能.