J'essaye de faire quelques animations en utilisant Core Animation sur l'iphone. J'utilise CABasicAnimation sur CALayer. C'est une animation directe d'un endroit aléatoire en haut de l'écran au bas de l'écran à une vitesse aléatoire, j'ai 30 éléments qui font la même animation en continu jusqu'à ce qu'une autre action se produise. Mais la performance sur l'iPhone 3G est très lente lorsque les animations commencent. L'image est seulement 8k.Core Animation performance sur iphone
Est-ce la bonne approche? Comment devrais-je changer afin qu'il fonctionne mieux.
// image cached somewhere else.
CGImageRef imageRef = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"png"]] CGImage];
- (void)animate:(NSTimer *)timer
{
int startX = round(radom() % 320);
float speed = 1/round(random() % 100 + 2);
CALayer *layer = [CALayer layer];
layer.name = @"layer";
layer.contents = imageRef; // cached image
layer.frame = CGRectMake(0, 0, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
int width = layer.frame.size.width;
int height = layer.frame.size.height;
layer.frame = CGRectMake(startX, self.view.frame.origin.y, width, height);
[effectLayer addSublayer:layer];
CGPoint start = CGPointMake(startX, 0);
CGPoint end = CGPointMake(startX, self.view.frame.size.height);
float repeatCount = 1e100;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.delegate = self;
animation.fromValue = [NSValue valueWithCGPoint:start];
animation.toValue = [NSValue valueWithCGPoint:end];
animation.duration = speed;
animation.repeatCount = repeatCount;
animation.autoreverses = NO;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
[layer addAnimation:animation forKey:@"position"];
}
Les animations sont déclenchées à l'aide d'un NSTimer.
animationTimer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(animate:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:animationTimer forMode:NSDefaultRunLoopMode];
Étape 1: exécutez-le sur un iPhone 3GS. ;-) –
lol. J'ai fait. et ça a bien fonctionné. – nico
mais la majorité des utilisateurs d'iphone sont encore sur la 3G bien que :) – nico