Search code examples
objective-cxcodecore-animationcalayer

Can´t get a smooth step by step rotation with CoreAnimation and CALayers


Im trying to rotate a CALayer with various sublayers according to time. There is UDP Multicast receiver in place which will receive new timecodes. I fetch new times via a timer:

 [NSTimer scheduledTimerWithTimeInterval:0.02f
 target:self
 selector:@selector(fetchNewTimeAndRotate)
 userInfo:nil
 repeats:YES];

The CALayer should be rotated according to time. In my case, one rotation in 1.8 seconds. Actually i dont need any animation, i just need the angle to be set very often, so that it produces the actual animation.

I´ve tried setting the layers rotation in various ways:

1st i tried via CATransform3DMakeRotation:

CATransform3D rotation1 = CATransform3DMakeRotation([self DegreesToRadians:newAngle], 0.0f, 0.0f, 1.0f);
self.circleLayer.transform=rotation1

2nd try was using an instant rotation via an animation:

rotation.fromValue = [NSNumber numberWithFloat:[self DegreesToRadians:oldAngle]];
rotation.toValue = [NSNumber numberWithFloat:[self DegreesToRadians:newAngle]];
rotation.duration = 0.0f;
rotation.repeatCount = 0.0f;
rotation.removedOnCompletion = NO; //also used YES here with no effect
[self.circleLayer addAnimation:rotation forKey:@"transform.rotation.z"];
oldAngle=newAngle;

3rd try was just setting an angle to "transform.rotation.z"

[self.circleLayer setValue:[NSNumber numberWithFloat:[self DegreesToRadians:newAngle]] forKeyPath:@"transform.rotation.z"];

All of the above approaches do work, but lead to significant stuttering in the rotation. I´ve tried using several different timings in fetching and in animation-length. Nothing seems to get rid of the problem. Using an autorotation every 0.02 and an 3.6° angle is the only option that presented smooth results for me. Am i making a fundamental mistake here or didn´t understand the concept of CoreAnimation? I know that it is to be used to make the animation itself. But i need it to react to user input and change immediatly. Im thankful for any help.


Solution

  • You mean CADisplayLink, don't you?

    DisplayLink objects are commonly used for frame-based animation in OpenGL, but they do not "use OpenGL". It's perfectly valid to create a display link and attach it to your application to trigger drawing.

    As the other poster said, NSTimer is not a good choice for fine control of animation frames. If you write a program that just runs a timer and measures it, it will look good. However, timers depend on your app visiting the event loop frequently. They are not preemptive, and if your app is busy when the timer should fire, it will be off, or even miss a firing interval.

    If you continue to use a display link, you might want to write your code to check the time elapsed from the last frame and calculate the amount to animated baed on that. That way if you drop a frame the animation still proceeds at the correct velocity (If you drop a frame then twice as much time elapses before the next frame, so you make your objects move twice as much.)