在我的应用程序中,我使用精灵表图像来显示动画。我为其中包含 33 个图像的图像制作了一个 png 图像。
但它有时显示非常快,有时显示良好的动画。
我不知道为什么会这样? 我认为这可能是线程的问题。 我的代码如下
-(void)correctanswer
{
[self setImage];
}
-(void) setImage
{
CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 180.0 * -3.30 );
[imgv setTransform:rotate];
layer=[[RMMSpriteLayer alloc]init];
layer = [RMMSpriteLayer layer];
CGImageRef img1;
CGSize size;
img1 = [[UIImage imageNamed:@"newflash.png"] CGImage];
size = CGSizeMake(640,1136);
if ([APPDELEGATE isPad]) {
[imganimation setFrame: CGRectMake(0,0,768,1024)];
}
else
{
[imganimation setFrame: CGRectMake(0,0,320,480)];
}
[layer setContents:(id)(img1)];
CGSize normalizedSize = CGSizeMake( size.width/CGImageGetWidth(img1), size.height/CGImageGetHeight(img1) );
layer.frame = CGRectMake(0,0,320,568);
if ([APPDELEGATE isPad]) {
[layer setFrame: CGRectMake(0,0,768,1024)];
}
else
{
// [layer setFrame: CGRectMake(0,0,320,568)];
[layer setFrame:CGRectMake(30, 48, 260, 260)];
}
layer.contentsRect = CGRectMake( 0, 0, normalizedSize.width, normalizedSize.height );
[[[self view] layer] addSublayer:layer];
[layer setHidden:NO];
[[self imganimation] setHidden:NO];
[self.view bringSubviewToFront:imganimation];
CGImageRelease(img1);
[self simulateMemoryWarning];
[self animationFlush];
}
- (void)animationFlush {
[imganimation setHidden:NO];
[self.view bringSubviewToFront:imganimation];
CABasicAnimation *anim=[[CABasicAnimation alloc]init];
// CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:1]; // initial frame
anim.toValue = [NSNumber numberWithInt:33]; // last frame + 1
//anim.toValue = [NSNumber numberWithInt:];
anim.duration = 20.0; // from the first frame to the 6th one in 1 second
anim.repeatCount = 0; // just keep repeating it
anim.autoreverses = NO; // do 1, 2, 3, 4, 5, 4, 3, 2
[self remove];
[layer addAnimation:anim forKey:nil];
//[layer release];
}