I am creating a musical instrument for the iPhone. In my app when i touch a CALayer a note plays and the layer does a wiggle for the duration of the note.
This was all working perfectly on iOS 4.1, however I just upgraded to iOS 5.0.1 and experienced major lag issues if I pressed multiple notes in succession. After much pain I have narrowed it down to the following wiggle animation code for the CALayer that is touched.
// here is an example wiggle
CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:@"transform"];
wiggle.duration = 0.1;
//wiggle.repeatCount = 1e100f;
wiggle.repeatCount = 100;
wiggle.autoreverses = YES;
wiggle.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(fret.fretLayer.transform,0.2, 0.0 ,1.0 ,2.0)]; //angle, x , y , z
wiggle.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(fret.fretLayer.transform,-0.2, 0.0 ,1.0 ,2.0)]; //angle, x , y , z
// doing the wiggle
[note.noteLayer addAnimation:wiggle forKey:@"wiggle"];
If I block out the last line where the animation is added to the layer all lag disappears immediately. It feels like the main thread is being blocked somehow, or its not running on the main thread, but I have tried invoking the function with performSelectorOnMainThread:withObject:waitUntilDone:NO and it made no difference. If I press loads of notes really quickly the whole app pauses, and then a second or so later its like it catches up with itself and suddenly all the sound plays and the animation finally begins all choppy and choked after that.
Does anyone know of any unusual issues with CABasicAnimation in iOS 5? Is there any alternative animation APIs that I could try that could also animate CALayers for an unspecified duration on repeat? Anyone have any suggestions or guesses as to how to fix/what the problem could stem from?
EDIT: I have determined it is definitely nothing to do with nature of the animation. I replaced the wiggle code with code that simply fades the colour like so, but I am still getting the same lag effect with notes being laggy to play.
CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
wiggle.duration = 2;
wiggle.toValue = (id)[UIColor whiteColor].CGColor ;
wiggle.fromValue = (id)[UIColor colorWithRed:0.1 green:0.2 blue:0.8 alpha:0.4].CGColor;
Suspect it could be something to do with a change in CoreAudio/RemoteIO perhaps? And that is interfering with the main thread or visa versa?
Finally worked out the true nature of what was causing the issue. Turns out there is a problem with using shouldRasterize = YES in iOS 5.
In my app I am embedding 100+ CALayers in a UIView which has had its layer set to shouldRasterize = YES. I created a new app from the ground up and added bits in until i replicated the problem. Ultimately it turned out to be this. In iOS 4 this isnt a problem. In iOS 5 it is. If I turn shouldRasterize off the problem disappears immediately.
Have submitted a bug report with apple.
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