CABasicAnimation, rotation, major slowdown

I have created an animated color wheel where the user navigates through different views and each time the wheel rotates to the next color. Each view is loaded with navigation buttons, sound buttons and pictures etc.

Everything works fine except that every time the animation is called the image remains in the background and eventually leads to massive slowdown of the animation.

The animation lasts around 1.8 seconds each time and the image used is 800x800 with around 320x400 of it being in view at any one time.

From what i can understand, after the animation has finished, it is not being released. I have tried removeFromSuperview but that means the next time i navigate to the next view its gone.

Here is the code, i have 8 screens, each with a back and next button to navigate. The first view displays a full rotating color wheel, which works perfectly and a start button.

#import "FlipsideView1.h"
#import "Flip1View1.h"
#import "Flip1View2.h"
#import "Flip1View3.h"
#import "Flip1View4.h"
#import "Flip1View5.h"
#import "Flip1View6.h"
#import "Flip1View7.h"
#import "Flip1View8.h"

@implementation FlipsideView1

-(void)awakeFromNib
{
[self addSubview:flip1View1];

[self performSelector:@selector(removeRainbowAnimation0) withObject:nil afterDelay:0.05];
}

- (void) removeRainbowAnimation0 {

UIImage *image = [UIImage imageNamed:@"Rainbow310.png"];

UIImageView *imgView1 =[[UIImageView alloc] initWithImage:image];
[imgView1 setFrame:(CGRect){{0,0},image.size}];
[imgView1 setCenter:(CGPoint){160,220}];
[flip1View1 addSubview:imgView1];
[imgView1 release];

CABasicAnimation *fullRotation = [CABasicAnimation     animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 7;
fullRotation.repeatCount = 1e100f;
fullRotation.removedOnCompletion = YES;
[imgView1.layer addAnimation:fullRotation forKey:@"360"];
}

-(void)flip1View2
{
[flip1View1 removeFromSuperview];

[self addSubview:flip1View2];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView2 =[[UIImageView alloc] initWithImage:image];
[imgView2 setFrame:(CGRect){{0,0},image.size}];
[imgView2 setCenter:(CGPoint){160,40}]; 
imgView2.transform = CGAffineTransformMakeRotation(1.73);
[self insertSubview:imgView2 belowSubview:flip1View2];
[imgView2 release];
}

-(void)flip1View2return
{
[flip1View2 removeFromSuperview];

[self addSubview:flip1View1];
}

-(void)flip1View3
{
[flip1View2 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView3 =[[UIImageView alloc] initWithImage:image];
[imgView3 setFrame:(CGRect){{0,0},image.size}];
[imgView3 setCenter:(CGPoint){160,40}];
[self addSubview:imgView3];
[imgView3 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:1.73];
fullRotation.toValue = [NSNumber numberWithFloat:2.63];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView3.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation1) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation1 {

[self addSubview:flip1View3];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView4 =[[UIImageView alloc] initWithImage:image];
[imgView4 setFrame:(CGRect){{0,0},image.size}];
[imgView4 setCenter:(CGPoint){160,40}];
imgView4.transform = CGAffineTransformMakeRotation(2.63);
[self insertSubview:imgView4 belowSubview:flip1View3];
[imgView4 release];
}

-(void)flip1View3return
{
[flip1View3 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView5 =[[UIImageView alloc] initWithImage:image];
[imgView5 setFrame:(CGRect){{0,0},image.size}];
[imgView5 setCenter:(CGPoint){160,40}];
[self addSubview:imgView5];
[imgView5 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:2.63];
fullRotation.toValue = [NSNumber numberWithFloat:1.73];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView5.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation11) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation11 {

[self addSubview:flip1View2];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView6 =[[UIImageView alloc] initWithImage:image];
[imgView6 setFrame:(CGRect){{0,0},image.size}];
[imgView6 setCenter:(CGPoint){160,40}]; 
imgView6.transform = CGAffineTransformMakeRotation(1.73);
[self insertSubview:imgView6 belowSubview:flip1View2];
[imgView6 release];
}

-(void)flip1View4
{
[flip1View3 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView7 =[[UIImageView alloc] initWithImage:image];
[imgView7 setFrame:(CGRect){{0,0},image.size}];
[imgView7 setCenter:(CGPoint){160,40}];
[self addSubview:imgView7];
[imgView7 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:2.63];
fullRotation.toValue = [NSNumber numberWithFloat:3.53];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView7.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation2) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation2 {

[self addSubview:flip1View4];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView8 =[[UIImageView alloc] initWithImage:image];
[imgView8 setFrame:(CGRect){{0,0},image.size}];
[imgView8 setCenter:(CGPoint){160,40}];
imgView8.transform = CGAffineTransformMakeRotation(3.53);
[self insertSubview:imgView8 belowSubview:flip1View4];
[imgView8 release];
}

-(void)flip1View4return
{
[flip1View4 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView9 =[[UIImageView alloc] initWithImage:image];
[imgView9 setFrame:(CGRect){{0,0},image.size}];
[imgView9 setCenter:(CGPoint){160,40}];
[self addSubview:imgView9];
[imgView9 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:3.53];
fullRotation.toValue = [NSNumber numberWithFloat:2.63];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView9.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation22) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation22 {

[self addSubview:flip1View3];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView10 =[[UIImageView alloc] initWithImage:image];
[imgView10 setFrame:(CGRect){{0,0},image.size}];
[imgView10 setCenter:(CGPoint){160,40}];
imgView10.transform = CGAffineTransformMakeRotation(2.63);
[self insertSubview:imgView10 belowSubview:flip1View3];
[imgView10 release];
}

-(void)flip1View5
{
[flip1View4 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView11 =[[UIImageView alloc] initWithImage:image];
[imgView11 setFrame:(CGRect){{0,0},image.size}];
[imgView11 setCenter:(CGPoint){160,40}];
[self addSubview:imgView11];
[imgView11 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:3.53];
fullRotation.toValue = [NSNumber numberWithFloat:4.43];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView11.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation3) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation3 {

[self addSubview:flip1View5];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView12 =[[UIImageView alloc] initWithImage:image];
[imgView12 setFrame:(CGRect){{0,0},image.size}];
[imgView12 setCenter:(CGPoint){160,40}];
imgView12.transform = CGAffineTransformMakeRotation(4.43);
[self insertSubview:imgView12 belowSubview:flip1View5];
[imgView12 release];
}

-(void)flip1View5return
{
[flip1View5 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView13 =[[UIImageView alloc] initWithImage:image];
[imgView13 setFrame:(CGRect){{0,0},image.size}];
[imgView13 setCenter:(CGPoint){160,40}];
[self addSubview:imgView13];
[imgView13 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:4.43];
fullRotation.toValue = [NSNumber numberWithFloat:3.53];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView13.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation33) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation33 {

[self addSubview:flip1View4];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView14 =[[UIImageView alloc] initWithImage:image];
[imgView14 setFrame:(CGRect){{0,0},image.size}];
[imgView14 setCenter:(CGPoint){160,40}];
imgView14.transform = CGAffineTransformMakeRotation(3.53);
[self insertSubview:imgView14 belowSubview:flip1View4];
[imgView14 release];
}

-(void)flip1View6
{
[flip1View5 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView15 =[[UIImageView alloc] initWithImage:image];
[imgView15 setFrame:(CGRect){{0,0},image.size}];
[imgView15 setCenter:(CGPoint){160,40}];
[self addSubview:imgView15];
[imgView15 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:4.43];
fullRotation.toValue = [NSNumber numberWithFloat:5.33];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView15.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation4) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation4 {

[self addSubview:flip1View6];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView16 =[[UIImageView alloc] initWithImage:image];
[imgView16 setFrame:(CGRect){{0,0},image.size}];
[imgView16 setCenter:(CGPoint){160,40}];
imgView16.transform = CGAffineTransformMakeRotation(5.33);
[self insertSubview:imgView16 belowSubview:flip1View6];
[imgView16 release];
}

-(void)flip1View6return
{
[flip1View6 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView17 =[[UIImageView alloc] initWithImage:image];
[imgView17 setFrame:(CGRect){{0,0},image.size}];
[imgView17 setCenter:(CGPoint){160,40}];
[self addSubview:imgView17];
[imgView17 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:5.33];
fullRotation.toValue = [NSNumber numberWithFloat:4.43];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView17.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation44) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation44 {

[self addSubview:flip1View5];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView18 =[[UIImageView alloc] initWithImage:image];
[imgView18 setFrame:(CGRect){{0,0},image.size}];
[imgView18 setCenter:(CGPoint){160,40}];
imgView18.transform = CGAffineTransformMakeRotation(4.43);
[self insertSubview:imgView18 belowSubview:flip1View5];
[imgView18 release];
}

-(void)flip1View7
{
[flip1View6 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView19 =[[UIImageView alloc] initWithImage:image];
[imgView19 setFrame:(CGRect){{0,0},image.size}];
[imgView19 setCenter:(CGPoint){160,40}];
[self addSubview:imgView19];
[imgView19 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:5.33];
fullRotation.toValue = [NSNumber numberWithFloat:6.23];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView19.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation5) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation5 {

[self addSubview:flip1View7];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView20 =[[UIImageView alloc] initWithImage:image];
[imgView20 setFrame:(CGRect){{0,0},image.size}];
[imgView20 setCenter:(CGPoint){160,40}];
imgView20.transform = CGAffineTransformMakeRotation(6.23);
[self insertSubview:imgView20 belowSubview:flip1View7];
[imgView20 release];
}

-(void)flip1View7return
{
[flip1View7 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView21 =[[UIImageView alloc] initWithImage:image];
[imgView21 setFrame:(CGRect){{0,0},image.size}];
[imgView21 setCenter:(CGPoint){160,40}];
[self addSubview:imgView21];
[imgView21 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:6.23];
fullRotation.toValue = [NSNumber numberWithFloat:5.33];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView21.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation55) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation55 {

[self addSubview:flip1View6];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView22 =[[UIImageView alloc] initWithImage:image];
[imgView22 setFrame:(CGRect){{0,0},image.size}];
[imgView22 setCenter:(CGPoint){160,40}];
imgView22.transform = CGAffineTransformMakeRotation(5.33);
[self insertSubview:imgView22 belowSubview:flip1View6];
[imgView22 release];
}

-(void)flip1View8
{
[flip1View7 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView23 =[[UIImageView alloc] initWithImage:image];
[imgView23 setFrame:(CGRect){{0,0},image.size}];
[imgView23 setCenter:(CGPoint){160,40}];
[self addSubview:imgView23];
[imgView23 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:6.23];
fullRotation.toValue = [NSNumber numberWithFloat:7.13];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView23.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation6) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation6 {

[self addSubview:flip1View8];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView24 =[[UIImageView alloc] initWithImage:image];
[imgView24 setFrame:(CGRect){{0,0},image.size}];
[imgView24 setCenter:(CGPoint){160,40}];
imgView24.transform = CGAffineTransformMakeRotation(7.13);
[self insertSubview:imgView24 belowSubview:flip1View8];
[imgView24 release];
}

-(void)flip1View8return
{
[flip1View8 removeFromSuperview];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView25 =[[UIImageView alloc] initWithImage:image];
[imgView25 setFrame:(CGRect){{0,0},image.size}];
[imgView25 setCenter:(CGPoint){160,40}];
[self addSubview:imgView25];
[imgView25 release];

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:7.13];
fullRotation.toValue = [NSNumber numberWithFloat:6.23];
fullRotation.duration = 1.8;
fullRotation.removedOnCompletion = YES;

[imgView25.layer addAnimation:fullRotation forKey:@"360"];

[self performSelector:@selector(removeRainbowAnimation66) withObject:nil afterDelay:1.75];
}

- (void) removeRainbowAnimation66 {

[self addSubview:flip1View7];

UIImage *image = [UIImage imageNamed:@"Rainbow10.png"];

UIImageView *imgView26 =[[UIImageView alloc] initWithImage:image];
[imgView26 setFrame:(CGRect){{0,0},image.size}];
[imgView26 setCenter:(CGPoint){160,40}];
imgView26.transform = CGAffineTransformMakeRotation(6.23);
[self insertSubview:imgView26 belowSubview:flip1View7];
[imgView26 release];
}

-(void)flip1View8home
{
[flip1View8 removeFromSuperview];

[self addSubview:flip1View1];
}

- (void)dealloc {
[super dealloc];
}

@end

This is my entire .m and I'm sorry if its alot of repeated code but I left it in to give a better idea of what I'm doing.

This all happens when the user selects one of four options on the opening screen.

Thank You.

Alex


Your allocing imgView in every method without ever releasing it again, this will create a leak. You can either do:

UIImageView *imgView =[[[UIImageView alloc] initWithImage:image] autorelease];

or release it yourself like

[self addSubview:imgView];
[imgView release];

You can check for leaks by going to Build -> Build and Analyse in the menu bar

链接地址: http://www.djcxy.com/p/74248.html

上一篇: CAT转换图层在旋转后的最终位置

下一篇: CABasicAnimation,旋转,主要放缓