我为图层编写动画,将图层从一个位置移动到另一个位置:
- (void) animateCentreRightTransition: (int) transition {
float scaleFactor[2] = {1, 0.8};
if (transition == WatchTransitionClockwisePortrait || transition == WatchTransitionClockwiseLandscape) {
scaleFactor[0] = 0.8;
scaleFactor[1] = 1;
}
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, kCentreRightTrasition[transition][0], kCentreRightTrasition[transition][1]);
CGPathAddCurveToPoint(curvedPath, NULL, kCentreRightTrasition[transition][2], kCentreRightTrasition[transition][3], kCentreRightTrasition[transition][4], kCentreRightTrasition[transition][5], kCentreRightTrasition[transition][6], kCentreRightTrasition[transition][7]);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath: @"transform.scale"];
scale.fromValue = [NSNumber numberWithDouble:scaleFactor[0]];
scale.toValue = [NSNumber numberWithDouble:scaleFactor[1]];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:scale, pathAnimation, nil];
group.autoreverses = NO;
group.duration = kWatchTransitionDuration;
group.repeatCount = 1;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
group.delegate = self;
[self.layer addAnimation:group forKey:@"animateCentreRightTransition"];
NSLog(@"Centre right %f", self.layer.position.x);
NSLog(@"%f", self.layer.position.y);
}
但看起来当我的图层转换到新位置时,它的位置属性保持不变?现在,当我旋转屏幕,并设置位置坐标以适应横向模式,结果不是我想要的,图层不会留在我想要的地方。我在这里缺少什么?
答案 0 :(得分:4)
动画不会更改图层的属性,因此通常一旦动画完成并从图层中移除,图层就会移回其初始状态。
如果希望图层在动画完成后保留动画中的最后位置,则需要在添加动画后立即显式设置图层的位置。例如:
[self.layer addAnimation:group forKey:@"animateCentreRightTransition"];
self.layer.position = somePoint;