I'm trying to create a 3D user interface using CALayer 3d transforms. I have figured out the basic technique for achieving this, but my real-time performance is pretty bad. Specifically, the user should be able to "pan" the interface "scene" interactively.
In the following snippet, "model" is the layer I'm trying to animate. "camera" is a CATransform3D matrix that I'm continuously updating by applying touch translations.
The approach works, but panning is very sluggish. If I uncomment the part with the CGAffineTransform, I'm getting fast and responsive panning - but I'm losing the change in perspective that should occur when panning.
- (void)didPan:(UIPanGestureRecognizer*)pan
{
if (pan.state==UIGestureRecognizerStateChanged)
{
CGPoint p = [pan translationInView:self.view.window];
camera = CATransform3DTranslate(camera, p.y, 0, -p.x);
model.transform = CATransform3DConcat(camera, modelView);
// CGAffineTransform tA = self.view.transform;
// tA = CGAffineTransformTranslate(tA, p.x, p.y);
// self.view.transform = tA;
[pan setTranslation:CGPointZero inView:self.view.window];
}
}
How can I improve rendering performance?
Okay, I solved it myself: the problem were implicit animations. Every model.transform = ... call triggered an implicit animation, resulting in a very sluggish behavior. Disabling implicit animations for the transform key, when creating the "model" layer, solved the problem:
model.actions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNull null], @"transform",
nil];
Now everything is snappy and fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With