New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
iOS Development Discussion on developing apps for the iOS platform.

Core Animation: Animations not smooth and judder/shudder - Help required


Post Reply New Thread Subscribe

 
Thread Tools
mja786

 
Member Since: Feb 21, 2012
Posts: 2
mja786 is on a distinguished road

mja786 is offline
I am new to IOS development, and will be grateful for any help.

I'm currently working with Core Animation. I have noticed that when I have several animations (either CABasicAnimation or CAKeyFrameAnimation) animating at the same time they judder and the layers become blurry. The more animations there are, animating simultaneously, the worse this problem is. Everything works fine in the IOS simulator on an iMac, the problem occurs when I test it on an iPhone.

I am using CALayer to create many layers then adding animations to those layers.

Here is a snippet of the code:

- (void) doAnimation
{

// Rotate shape about z axis
CABasicAnimation *rotation;
rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.duration = 10.0;
rotation.beginTime = CACurrentMediaTime();
[rotation setToValueNSNumber numberWithFloat:32]];



//Blue Square
CALayer *squareLayer = [CALayer layer];
squareLayer.backgroundColor = [UIColor blueColor].CGColor;
squareLayer.shadowOffset = CGSizeMake(0, 3);
squareLayer.shadowRadius = 5.0;
squareLayer.shadowColor = [UIColor blackColor].CGColor;
squareLayer.shadowOpacity = 0.8;
squareLayer.frame = CGRectMake(-70, 50, 50, 50);


// Move Blue Square
CABasicAnimation *animation1;
animation1 = [CABasicAnimation animationWithKeyPath:@"position"];

animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(-80, 50)];
animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(400, 50)];
animation1.duration = 4.0;
animation1.beginTime = CACurrentMediaTime();

//Add squareLayer to the view
[self.view.layer addSublayer:squareLayer];

//Add animations to squareLayer
[squareLayer addAnimation:rotation forKey:@"transform"];
[squareLayer addAnimation:animation1 forKey:@"position"];



//Create a UIBezierPath for a Triangle
UIGraphicsBeginImageContextWithOptions(CGSizeMake( 200,200), NO, 0);

UIBezierPath *trianglePath = [UIBezierPath bezierPath];

[trianglePath moveToPoint:CGPointMake(100, 25)];
[trianglePath addLineToPoint:CGPointMake(165, 137.58)];
[trianglePath addLineToPoint:CGPointMake(35, 137.58)];
[trianglePath closePath];

//Fill the UIBezierPath with a color
[[UIColor magentaColor]setFill];
[trianglePath fill];

//Create a UIImage using the current context.
UIImage *triangle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Create a layer for triangle, use the UIImage triangle
CALayer *triangleLayer = [CALayer layer];
triangleLayer.frame = CGRectMake(-100, 150, 100, 100);
triangleLayer.contents = (id)triangle.CGImage;

triangleLayer.shadowOffset = CGSizeMake(0,3);
triangleLayer.shadowRadius = 5.0;
triangleLayer.shadowColor = [UIColor blackColor].CGColor;
triangleLayer.shadowOpacity = 0.8;

//Move Triangle Animation
CABasicAnimation *animation2;
animation2 = [CABasicAnimation animationWithKeyPath:@"position"];
animation2.fromValue = [NSValue valueWithCGPoint:CGPointMake(-100, 150)];
animation2.toValue = [NSValue valueWithCGPoint:CGPointMake(400, 150)];
animation2.duration = 4.0;
animation2.beginTime = CACurrentMediaTime();

//Add triangleLayer to the view
[self.view.layer addSublayer:triangleLayer];

//Add animations to triangleLayer
[triangleLayer addAnimation:rotation forKey:@"transform"];
[triangleLayer addAnimation:animation2 forKey:@"position"];

}

As you can see, there is a squareLayer and a triangleLayer to which I add animations. If I set the animations to animate one after the other i.e.
animation2.begintime = CACurrentMediaTime() + 5.0; the animations are very smooth - but if the animations remain as they are coded above (animating simultaneously), they are no longer smooth - and judder. As I said, the problem is on the iPhone itself not on the IOS simulator on a Mac. It gets worse as I add more animations to the doAnimation method.

Any advice will be appreciated.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
I hav'nt done to much work with Core Animation myself, but I would start out saying that
the iOS operating system will never be able to perform as quickly as the Mac, it has a lot
slower processor, and a lot less RAM and Graphics performance.

After a quick through the documentation I did see that the CAAnimationGroup class might
help with your performance problem, but I cant promise this.
You use it by adding all of your CABasicAnimations to CAAnimation Group object like this.

Code:
CABasicAnimation *mover = [CABasicAnimation animationWithKeyPath: @"position"];
[mover setDuration:1.0];
[mover setFromValue:[NSValue valueWithCGPoint: CGPointMake(0.0, 100.0)]];
[mover setToValue:[NSValue valueWithCGPoint: CGPointMake(100.0, 100.0)]];

CABasicAnimation *fader = [CABasicAnimation animationWithKeyPath: @"opacity"];
[mover setDuration:1.0];
[mover setFromValue:[NSNumber numberWithFloat: 1.0]];
[mover setToValue:[NSNumber numberWithFloat: 1.0]];

CAAnimationGroup *group = [CAAnimationGroup animation];
[group setAnimations: [NSArray arrayWithObjects: mover, fader, nil]];

[[[self view] layer] addSubLayer: group];
This example code does not do anything, because I have not created any objects to animate, but it serves only to show how to use the CAAnimationGroup class.
Not sure this class will help performance, as I have not tested it on an iOS device.,
but might be worth trying.

The only other thing worth try is to run the different animations in seperate threads, but this
is not always advised with iOS devices, where limited system resources are available.

Not sure if this helps much, but good luck with it.

Regards Mark
QUOTE Thanks

Post Reply New Thread Subscribe


« Scrollbar in UIWebView | Starting to make apps »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

All times are GMT -4. The time now is 08:07 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?