Iphone 如何使用CATTransferM3DMakeRotation从左向右旋转视图
我知道如何从右向左旋转视图Iphone 如何使用CATTransferM3DMakeRotation从左向右旋转视图,iphone,ios,calayer,catransform3d,Iphone,Ios,Calayer,Catransform3d,我知道如何从右向左旋转视图 CATransform3D transform = CATransform3DIdentity; transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f); transform.m34 = 0.0015; view.layer.transform = transform; 但是我不能从左向右旋转它。 请帮帮我。我在函数文档中没有找到任何内容,但我认为您可以将旋转角度设置为负值,以旋转到另一个方向。如果
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
transform.m34 = 0.0015;
view.layer.transform = transform;
但是我不能从左向右旋转它。
请帮帮我。我在函数文档中没有找到任何内容,但我认为您可以将旋转角度设置为负值,以旋转到另一个方向。如果要旋转视图,请返回初始位置。。。只需使用:
transform = CATransform3DMakeRotation(0, 0.0f, 1.0f, 0.0f)
或者你可以“组合”更多
你可以试试。。。使用
CATTransformM3D transform=starLayer.transform代码>而不是CATTransferM3D transform=CATTransferM3DIDEntity代码>我不知道你说的“从左到右”和“从右到左”是什么意思。无论如何,正如您所发现的,您不能仅仅通过设置变换矩阵来控制动画的方向,因为-M_PI和+M_PI具有相同的最终效果,因此核心动画无法可靠地知道您希望它以哪种方式旋转
相反。这样做有点复杂,特别是因为您希望为视图的层而不是独立层(不受视图控制)设置动画
总之,下面是我测试的代码:
- (IBAction)rotateButtonWasTapped:(id)sender {
// Establish the perspective to be used during the animation.
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 0.0015;
self.imageView.layer.transform = transform;
static CGFloat kAngle = -M_PI;
[CATransaction begin]; {
// After the animation completes, make the transform “permanent”. Otherwise it
// will be undone when the animation is removed from the layer.
[CATransaction setCompletionBlock:^{
self.imageView.layer.transform = CATransform3DRotate(transform, kAngle, 0, 1, 0);
}];
// Animate the rotation using Core Animation's extension to Key Value Coding.
// The sign of kAngle determines the direction of rotation.
CABasicAnimation *animation = [CABasicAnimation
animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = @0;
animation.toValue = @(kAngle);
animation.duration = 1;
[self.imageView.layer addAnimation:animation forKey:animation.keyPath];
} [CATransaction commit];
}
这将旋转视图,使视图的左边缘在旋转过程中“更靠近”用户。如果这不是“从左到右”的意思,请将kAngle
的定义更改为M_PI
,它将朝另一个方向旋转。我已经测试了两种方法。甚至可以在一个动画中多次旋转层。例如,尝试将kAngle
定义为-3*M_PI
-(void)updateTimer
{
如果(updatery this:transform=CATTransferorM3dMakeRotation(M_PI,0.0,0.0,1.0);这将反向旋转,我需要一个翻页动画。例如打开一本书(从右到左旋转)和关闭一本书(从左到右)。CATTransferorM3dMakeRotation(M_PI,0.0f,-1.0f,0.0f)正在进行相同的旋转,即从右到左。@ViratNaithani尝试CATTransferorM3dMakeRotation(-M_-PI,0.0f,1.0f,0.0f);@ViratNaithani可能需要两个步骤:第一步到(-M_-PI,0.0f,1.0f,0.0f)和最后一步(0,0.0f,1.0f,0.0f)@TonyMkenu上述两种旋转变换的组合是从左到右旋转。但是第一次旋转在最后并不干净。嗨,Rob,谢谢你的帮助,你的代码正常工作。“从左到右”表示视图从左方向旋转(如合上一本书).和animation.fromValue应该是0而不是@0,toValue应该是[NSNumber numberWithFloat:-kAngle];,对吗?fromValue和toValue都必须是对象。@0是表示零的NSNumber,但只是普通的0是空指针。我知道您已经接受了另一个答案。。而且还可以,这是一段很好的“代码”:对于这个答案+1。只是出于好奇,我的代码(上面)没有按照您的要求工作?
- (IBAction)rotateButtonWasTapped:(id)sender {
// Establish the perspective to be used during the animation.
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 0.0015;
self.imageView.layer.transform = transform;
static CGFloat kAngle = -M_PI;
[CATransaction begin]; {
// After the animation completes, make the transform “permanent”. Otherwise it
// will be undone when the animation is removed from the layer.
[CATransaction setCompletionBlock:^{
self.imageView.layer.transform = CATransform3DRotate(transform, kAngle, 0, 1, 0);
}];
// Animate the rotation using Core Animation's extension to Key Value Coding.
// The sign of kAngle determines the direction of rotation.
CABasicAnimation *animation = [CABasicAnimation
animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = @0;
animation.toValue = @(kAngle);
animation.duration = 1;
[self.imageView.layer addAnimation:animation forKey:animation.keyPath];
} [CATransaction commit];
}
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval interval = now - start;
int inter = (int)(interval*update);
inter*=10;
milliSec = (inter) % 1000;
seconds = (inter/1000) % 60;
minutes = (inter/60000) % 60;
timeValue = oldTimeValue+inter;
// Set Digital clock
// Set Analog clock hands
secHand.layer.transform = CATransform3DMakeRotation (Degrees2Radians(-(timeValue%1000)*360/1000), 0, 0, 1);