CAShapeLayer Slow user interaction

I have a CAShapeLayer, and it should perform the simple task of moving around the screen, guided by the user's finger.

The problem is that the movement is too slow. The layer moves, but there is a lag, and it feels slow.

I have another test application in which UIImage moves and there is no lag, and the image instantly moves.

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!

+4
source share
2 answers

Keep in mind that when you set a position on a layer (assuming that this is not the root level of the UIView by which the action is disabled by default), it implicitly animates the new position, which takes 0.25 seconds. If you want to make it faster, temporarily disable actions on this layer as follows:

- (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 lead to the fact that he will move to a new position, and not to the animation. If this does not help, let me take a look at your layer initialization code so that I can see what properties and sizes it has. Properties such as cornerRadius, for example, can affect performance.

+14
source

Try setting shouldRasterize to YES on the CAShapeLayer , especially if it is usually drawn at the same scale. If your application runs on high DPI devices, you may also need to set rasterizationScale to match the contentsScale layers.

While rasterizing your shape can speed up the movement of your shape, you probably want to temporarily disable rasterization while you are animating the path or size of the layers.

+2
source

Source: https://habr.com/ru/post/1339911/


All Articles