CABasicAnimation,旋转,主要放缓

我创建了一个动画色轮,用户通过不同的视图导航,每次轮子旋转到下一个颜色。 每个视图都加载了导航按钮,声音按钮和图片等。

一切正常,但每次动画被调用时,图像仍保留在背景中,并最终导致动画的大量放缓。

动画每次持续约1.8秒,使用的图像为800x800,其中任何时候都可以看到320x400左右的图像。

据我所知,动画完成后,它不会被释放。 我已经尝试removeFromSuperview,但这意味着下一次我导航到下一个视图已经消失。

这里是代码,我有8个屏幕,每个屏幕都有一个后退和下一个按钮来导航。 第一个视图显示一个完整的旋转色轮,完美的工作和一个开始按钮。

#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

这是我的整个.m,如果它有很多重复的代码,我很抱歉,但是我留下来让我更好地了解我在做什么。

这一切都发生在用户在开始屏幕上选择四个选项中的一个时。

谢谢。

亚历克斯


你在每个方法中分配imgView而没有再次释放它,这会造成泄漏。 你可以做:

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

或者像你自己释放它

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

您可以通过菜单栏中的Build -> Build and Analyse来检查泄漏情况

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

上一篇: CABasicAnimation, rotation, major slowdown

下一篇: CAShapeLayer slowing down interface rotation