I am working with Core Core Animation for the first time and in the process of implementing a game card that I can turn over, I decided to use CALayer
to display the content (not sure how I "I'm going to get two sides, but that's another question), and I need be able to flip it, move it, etc.
I use CATransaction
with some success - in the fragment below, the map moves from the lower left to the upper left corner and flips. The problem is that I donβt want to turn it over, but I donβt know how to say it: "hey, you are going wrong!"
[CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration]; myCard.position = CGPointMake(CGRectGetMidX(self.bounds)/2,CGRectGetMidY(self.bounds)/2); myCard.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0); [CATransaction commit];
Second question: how can I get it to do two conversions at the same time? I tried to CATransactions
two CATransactions
, but the second only overrides the first. I also tried changing the vector for rotation as 2D, for example, saying to rotate around the x and y axes, but this is not equivalent to simply translating it to pi around two separate axes. Here is the nested code.
[CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration]; myCard.position = CGPointMake(CGRectGetMidX(self.bounds)/2,CGRectGetMidY(self.bounds)/2); myCard.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);
And here it is inside the UIView
animation UIView
... I added sliders for the angle, x, y, z for the rotation vector and t for time. The translation is performed with time = 2t, and each of the turns should take only t each.
[CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:t * 2] forKey:kCATransactionAnimationDuration]; myCard.position = CGPointMake(CGRectGetMidX(self.view.bounds)/2,CGRectGetMidY(self.view.bounds)/2); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:t]; myCard.transform = CATransform3DMakeRotation(angle, x, y, z); [UIView commitAnimations]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:t]; [UIView setAnimationDelay:t]; myCard.transform = CATransform3DMakeRotation(angle * 2, x, y, z); [UIView commitAnimations]; [CATransaction commit];
And this is where I am now: everything works with one exception. Rotation y changes direction (starts to rotate in the opposite direction) when the map reaches pi / 2 and 3 * pi / 2. It also flips around the x axis at these points. But x and z work fine. So close!
CGMutablePathRef thePath = CGPathCreateMutable(); CGPathMoveToPoint(thePath,NULL,CGRectGetMidX(self.view.bounds)/2,CGRectGetMidY(self.view.bounds)/2*3); CGPathAddCurveToPoint(thePath,NULL, CGRectGetMidX(self.view.bounds)/2,CGRectGetMidY(self.view.bounds)/2*2, CGRectGetMidX(self.view.bounds)/2,CGRectGetMidY(self.view.bounds)/2*1.5, CGRectGetMidX(self.view.bounds)/2,CGRectGetMidY(self.view.bounds)/2); CAKeyframeAnimation *moveAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"]; moveAnimation.path=thePath; CFRelease(thePath); CABasicAnimation *xRotation; xRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"]; xRotation.fromValue = [NSNumber numberWithFloat:0.0]; xRotation.toValue = [NSNumber numberWithFloat:x * angle * M_PI]; CABasicAnimation *yRotation; yRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"]; yRotation.fromValue = [NSNumber numberWithFloat:0.0]; yRotation.toValue = [NSNumber numberWithFloat:y * angle * M_PI]; CABasicAnimation *zRotation; zRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; zRotation.fromValue = [NSNumber numberWithFloat:0.0]; zRotation.toValue = [NSNumber numberWithFloat:z * angle * M_PI]; CAAnimationGroup *groupAnimation = [CAAnimationGroup animation]; groupAnimation.duration = t; groupAnimation.removedOnCompletion = NO; groupAnimation.fillMode = kCAFillModeForwards; groupAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; groupAnimation.animations = [NSArray arrayWithObjects:moveAnimation, xRotation, yRotation, zRotation, nil]; [myCard addAnimation:groupAnimation forKey:@"animateCard"];