CABasicAnimation how to facilitate

I am currently using the following animation on a UITableViewCell :

 CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0); CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; rotationAnimation.duration = 0.25f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1; [cell.rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

However, when ~ 3 cells are animated, as above, the animations become very lagged. Is there a way to reduce this lag?

+6
source share
1 answer

The first thing I would like to remove the animation creation code from the -tableView:cellForRowAtIndexPath: method is to (say) viewDidLoad . Then add the animation to the cell in the -tableView:cellForRowAtIndexPath: method.

Creating objects and calculating the matrix is ​​expensive, so executing them for each call to -tableView:cellForRowAtIndexPath: will slow down your code.

In the code, I will have something similar to the following:

 - (void) viewDidLoad { // Normal viewDidLoad code above ... // Assume that rotationAnimation is an instance variable of type CABasicAnimation*; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0); rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; rotationAnimation.duration = 0.25f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // create cell ... // Now apply the animation to the necessary layer. [cell.rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; return cell; } 

Does it do this?

+1
source

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


All Articles