我知道如何从右到左旋转视图
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;
但我不能从左到右旋转它。请帮我。
我不确定您所说的“从左到右”和“从右到左”是什么意思。无论如何,正如您所发现的,您无法仅通过设置变换矩阵来控制动画的方向,因为 -M_PI 和 +M_PI 具有相同的最终效果,因此Core Animation无法可靠地知道您选择哪种方式想让它旋转。
相反,您可以为图层的关键路径设置动画transform.rotation.y
。这有点复杂,特别是因为您想要动画视图的图层而不是独立图层(不受视图控制)。
无论如何,这是我测试的代码:
- (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
to的定义,M_PI
它将向另一个方向旋转。我已经测试了两种方式。您甚至可以在一个动画中多次旋转图层。例如,尝试定义kAngle
为-3 * M_PI
.
我在函数文档中没有找到任何内容,但我认为您可以将旋转角度设为负值以旋转到另一个方向。
如果你想旋转视图,回到初始位置......只需这样做:
transform = CATransform3DMakeRotation(0, 0.0f, 1.0f, 0.0f)
或者你可以“组合”更多..
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DMakeRotation(-M_PI_2, 0.0f, 1.0f, 0.0f);
transform = CATransform3DMakeRotation(0, 0.0f, 1.0f, 0.0f);
transform.m34 = 0.0015;
view.layer.transform = transform;
您可以尝试...使用CATransform3D transform = starLayer.transform;
而不是CATransform3D transform = CATransform3DIdentity;
-(void)updateTimer { if(update<125) update=update+1;
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);
}