Search code examples
uikitcore-animationquartz-graphicscgpathcashapelayer

CAShapeLayer Slow User Interaction


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!


Solution

  • 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.