我想围绕屏幕的右边缘旋转一个 View ,就像开门一样。这是我目前所拥有的:
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0f/500;
view.layer.transform = CATransform3DRotate(transform, kDegreesToRadians(90), 0, 1, 0);
[UIView animateWithDuration:2.0f animations:^{
view.layer.transform = transform;
} completion:nil];
我对 CATranforms 或矩阵还不是很了解,所以如果有人能将我推向正确的方向,我将不胜感激!
最佳答案
UIView 动画用于为 View 的属性设置动画,而不用于为图层设置动画。据我所知,您不能使用 UIView animateWithDuration: 调用为 View 层设置动画。
您需要创建一个 CABasicAnimation 并将其添加到您自己的 View 层。
自从我完成 CAAnimations 以来已经有一段时间了,但让我们看看我能挖掘出什么:
正如 atxe 所说,在制作这个动画之前,你应该将图层的 anchor 移动到 1,.5,这样它就会围绕右边缘旋转。
这是我们的“Kevin & Kell”卡通应用程序中的一些代码,它可以制作门在铰链上打开的动画:
//Open the door.
rotateAnimation = [CABasicAnimation animation];
rotateAnimation.keyPath = @"transform";
rotateAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//Pivot on the right edge (y axis rotation)
theTransform = CATransform3DIdentity;
theTransform.m34 = -1/100.0;
theTransform = CATransform3DRotate(theTransform, degreesToRadians(70), 0, 1, 0);
rotateAnimation.toValue = [NSValue valueWithCATransform3D:theTransform];
rotateAnimation.duration = 0.5;
// leaves presentation layer in final state; preventing snap-back to original state
rotateAnimation.removedOnCompletion = NO;
rotateAnimation.fillMode = kCAFillModeBoth;
rotateAnimation.repeatCount = 0;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotateAnimation.delegate = self;
[treeDoorImageView.layer addAnimation:rotateAnimation forKey:@"transform"];
转换逻辑几乎与您的相同(我使用 m34 值 -1/100 以获得更夸张的 3D 效果。您的 -1/500 设置更正常。)
请注意,我没有尝试从 UIView 动画 block 执行动画,而是创建了一个 CABasicAnimation,并将其 fromValue 和 toValue 属性设置为开始和结束转换值。
您应该能够将此代码用作动画的起点。
关于ios - 围绕屏幕右侧旋转 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553753/