简体   繁体   中英

iOS 5 exc_bad_access code=2 with CABasicAnimation

I'm getting this error:

在此输入图像描述

With this code:

-(void)lowerGUI {
[mainGUI.layer addAnimation:guiLower forKey:@"transform.translation.y"];

}

I have a UIView which I'm animating up and down to move it out of the way if needed. I've set up my animation in viewDidLoad:

guiLower = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
guiLower.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
guiLower.duration = 1;
guiLower.fromValue = [NSNumber numberWithFloat:320];
guiLower.toValue = [NSNumber numberWithFloat:481];

The code builds and runs fine, but when I click the button to run the animation, the error appears.

Any ideas?

Thank you.

Chances are that you should hold a reference to your animation:

guiLower = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

in an ivar or property of your class and make sure that the object you assign to it is properly retained. Indeed, EXC_BAD_ACCESS is commonly caused by accessing an already deallocated instance of an object.

Example:

in .h file:

@property (nonatomic, retain) CABasicAnimation* guiLower;

in .m file:

@synthesize guiLower;

...

self.guiLower = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM