我正在与一些核心动画的第一次,并在实施一个纸牌,我可以翻转的过程中,我决定使用CALayer
显示内容(不知道我怎么'我会得到双方,但这是另一个问题),我需要能够翻转它,移动它等...如何使CATransform3dMakeRotation以另一种方式旋转?并链接在一起
我使用CATransaction
一些成功 - 在下面的代码片段中,卡片从左下角移动到左上角并翻转。问题是我不想翻转相反的方式,但不知道该怎么说,“嘿,你走错路了!”
[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];
第二个问题是:我怎么才能得到它同时做两个转换?我尝试过嵌套两个CATransactions
,但第二个刚好覆盖第一个。我也尝试将旋转矢量更改为2D,就像说绕x轴和y轴旋转一样,但这不等于只是围绕两个单独的轴翻转它。这是嵌套的代码。
[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); // rotate about x
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:1.0f] forKey:kCATransactionAnimationDuration];
myCard.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0); // rotate about y
[CATransaction commit];
[CATransaction commit];
这里,它是UIView
动画块内部...我已经添加滑块的角度,X,Y,Z的旋转向量和t为时间。翻译发生在时间上= 2t,并且每个旋转应该都只需要t。
[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];
这就是我现在所在:它所有的作品有一个例外。当卡达到pi/2和3 * pi/2时,y旋转反转(开始以相反的方向旋转)。它也在这些点上绕x轴翻转。但是x和z很好。很近!
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"];
谢谢布拉德。我找到了一个类似的答案(不知道是哪个 - 我现在在工作......),它使用了CAKeyframeAnimation,并有旋转的中间值,这样可以保证动画通过所有关键值。它可以工作,并且可以通过设置开始和结束的方式绕着所有坐标轴旋转我的卡片,就像你想象的那样,中间停止一半。实际上,它适用于x和z,但是对于y,例如在2 * M_PI ,当它到达M_PI时,它会翻转并做另一次旋转,我认为这可能是同样的事情,我需要更多的点 – Steve 2010-09-27 16:37:57
哦,我不能让它留在它结束的地方,我记得'CA'从WWDC视频中解决这个问题的人,但不记得如何去做请注意,是否可以执行'CATransaction'并将“myCard”的变换设置回标识以删除任何剩余的变换? (假设我可以让他们坚持!) – Steve 2010-09-27 16:39:59
编辑:在我的第一个评论中,我错了(刚刚用我的手机上的应用程序)。它在y旋转通过M_PI/2,M_PI和3 * M_PI/2到达2 * M_PI的路上时翻转。 – Steve 2010-09-27 16:44:01