Reputation: 8918
I have written animation for layer, that moves layer from one position to another:
- (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);
}
But what it seems is that while my layer is transitioned to new place, its position property stays the same? Now when i rotate the screen, and set position coordinates to fit the landscape mode, the result is not what i want, layer doesnt stay where i want it. What am i missing here?
Upvotes: 2
Views: 1048
Reputation: 4963
The animations do not change the properties of the layer, therefore usually the layer moves back to its initial state once the animation is finished and removed from the layer.
If you want the layer to keep its last position in the animation after the animation is finished, you need to set the position of the layer to that point explicitly right after you add the animation. For example:
[self.layer addAnimation:group forKey:@"animateCentreRightTransition"];
self.layer.position = somePoint;
Upvotes: 4