CAAnimations序列 - 图层返回到后续调用之间的初始位置

时间:2010-09-24 17:10:45

标签: iphone core-animation core-graphics

我正在尝试创建一个无限动画的图层向左轻微旋转,然后稍微向右和向后旋转。所以我在左边动画10度,然后在右边20点,然后再向左10点。 它在模拟器上运行良好,但在设备上该层“颤抖”。看起来每次开始下一个动画之前它都会返回到先前的状态。 我用

读过

animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

在这种情况下有所帮助,但是如果我使用它们,整个图层在第一次动画后就会冻结,并且永远不会移动到任何地方。

所以这是代码:

- (void) initAnimations {
    float baseValue = [[self.layer valueForKey:@"transform.rotation.z"] floatValue];

    rotateLeft = [[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"] retain];
    rotateLeft.fromValue = [NSNumber numberWithFloat:baseValue];
    rotateLeft.toValue = [NSNumber numberWithFloat:baseValue - DegreesToRadians(10.0f)];
    rotateLeft.duration = 0.4f;
    rotateLeft.removedOnCompletion = NO;
    rotateLeft.fillMode = kCAFillModeForwards;
    rotateLeft.repeatCount = 0;
    rotateLeft.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotateLeft.delegate = self;

    rotateRight = [[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"] retain];
    rotateRight.fromValue = [NSNumber numberWithFloat:baseValue - DegreesToRadians(10.0f)];
    rotateRight.toValue = [NSNumber numberWithFloat:baseValue + DegreesToRadians(10.0f)];
    rotateRight.duration = 0.6f;
    rotateRight.removedOnCompletion = NO;
    rotateRight.fillMode = kCAFillModeForwards;
    rotateRight.repeatCount = 0;
    rotateRight.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotateRight.delegate = self;

    rotateRestore = [[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"] retain];
    rotateRestore.fromValue = [NSNumber numberWithFloat:baseValue + DegreesToRadians(10.0f)];
    rotateRestore.toValue = [NSNumber numberWithFloat:baseValue];
    rotateRestore.duration = 0.3f;
    rotateRestore.removedOnCompletion = NO;
    rotateRestore.fillMode = kCAFillModeForwards;
    rotateRestore.repeatCount = 0;
    rotateRestore.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    rotateRestore.delegate = self;
}

- (void) beginAnimations {
    if (animationsRunning)
        return;

    [self.layer addAnimation:rotateLeft forKey:nil];
}

- (void)animationDidStart:(CAAnimation *)theAnimation {

}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    [self.layer removeAllAnimations];
    if (self.superview == nil) {
        rotateLeft.delegate = nil;
        rotateRight.delegate = nil;
        rotateRestore.delegate = nil;
        return;
    }

    float dt;
    if (step == 0) {
        dt = 0.7f + (float)(random() % 3) / 10.0f;
    } else {
        dt = 0.4f + (float)(random() % 3) / 10.0f;
    }

    CABasicAnimation* nextAnimation = nil;
    if (step == 0) {
        nextAnimation = rotateRight;
    } else if (step == 1) {
        nextAnimation = rotateRestore;
    } else if (step == 2) {
        nextAnimation = rotateLeft;
    }

    nextAnimation.duration = dt;
    [self.layer addAnimation:nextAnimation forKey:nil];

    step++;
    if (step == 3)
        step = 0;
}

为什么不工作?我想我已经尝试了所有removeOnCompletion / fillMode组合,似乎没有任何帮助=(

0 个答案:

没有答案