为什么我的动画在iPhone上闪烁?

时间:2010-10-08 03:48:04

标签: iphone graphics core-animation

我从下面的动画中得到随机的闪烁 - 看起来这张照片在继续它应该做的事情之前大约在1帧左右居中,我不知道为什么。

-(void)generateWander{
//NSLog(@"generateWander");

if(targetChange==0)
{
    targetChange=TARGET_LENGTH+1;
    float destinationAngle=((float)random()/RAND_MAX)*2*M_PI;
    NSLog(@"new destination:  %f",destinationAngle);
    targetY=sin(destinationAngle)*TARGET_RADIUS;
    targetX=cos(destinationAngle)*TARGET_RADIUS;
    NSLog(@"new target is: %f %f",targetX,targetY);
}
targetChange--;
float newVectorX=(targetX-currentX)*VECTOR_SCALAR;
float newVectorY=(targetY-currentY)*VECTOR_SCALAR;
vectorX+=newVectorX;
vectorY+=newVectorY;
if (pow((vectorX*vectorX+vectorY*vectorY),.5f)>MAX_SPEED) {
    vectorX*=.5;
    vectorY*=.5;
}
float newX=currentX+vectorX;
float newY=currentY+vectorY;
//NSLog(@"New position is: %f %f",newX,newY);
self.wander=[CAKeyframeAnimation 
             animationWithKeyPath:@"transform"];
NSValue *initial = [NSValue valueWithCATransform3D:
                    CATransform3DMakeTranslation(currentX, currentY, 0.0)];
NSValue *final = [NSValue valueWithCATransform3D:
                  CATransform3DMakeTranslation(newX, newY, 0.0)];
self.wander.values = [NSArray arrayWithObjects:initial, final, nil];
wander.removedOnCompletion=NO;
wander.duration = ANIMATION_TIME;
[self.wander setDelegate:self];
[wander setDelegate:self];
[self.icon removeAnimationForKey:@"wander"];
[self.icon addAnimation:self.wander forKey:@"wander"];
currentX=newX;
currentY=newY;
/*currentX=0.0F;
currentY=0.0F;
vectorX=0.0F;
vectorY=0.0F;
targetX=0.0F;
targetY=0.0F;
targetChange=0;*/   
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
//NSLog(@"Animation Did Stop was called");
if (theAnimation==[backgroundRays animationForKey:@"rotate"]) {
    //NSLog(@"rotate was detected");
    [backgroundRays removeAnimationForKey:@"rotate"];//May not be necessary, but I think it helps prevent a memory leak
    [self.backgroundRays addAnimation:self.rotate forKey:@"rotate"];
}
else if(theAnimation==[icon animationForKey:@"wander"]){
    [self generateWander];
}
}

0 个答案:

没有答案