ios – 如何在透视图中围绕其中心旋转平面对象?

我想要做的似乎应该很容易:我创建了一个旧的留声机记录的2D自上而下的视图。我想旋转它(放回)在其X轴,然后围绕其Z轴旋转。

我读过每一个问题,在CATransform3D的身体,我已经阅读Steve Baker的“Matrices can be your friends”文章以及Bill Dudney的书“Core Animation for Mac OS X和iPhone”我认为Brad Larson的“3-D Rotation without a trackball”有所有正确的代码,但由于他允许用户调整所有三个轴,我很难把他的代码收缩成我认为只是一个维度(旋转的z轴)。

这里是我测试的图像,而不是细节是重要的问题:

我带来的屏幕上通常的方式:(在UIView的子类)

- (void)awakeFromNib
{
    UIImage *recordImage = [UIImage imageNamed:@"3DgoldRecord"];
    if (recordImage) {
        recordLayer = [CALayer layer];
        [recordLayer setFrame:CGRectMake(0.0, 0.0, 1024, 1024)];
        [recordLayer setContents:(id)[[UIImage imageNamed:@"3DgoldRecord"] CGImage]];
        [self.layer addSublayer:recordLayer];
    }
}

这是第一个测试,只是把它在屏幕上;-)

然后,为了“回放”,我应用一个变换围绕图层的X轴旋转,在将图层的内容设置为图像之后,在添加子图层之前插入此代码:

    CATransform3D myRotationTransform = 
                    CATransform3DRotate(recordLayer.transform,
                                        (M_PI_2 * 0.85), //experiment with flatness
                                        1.0, // rotate only across the x-axis
                                        0.0, // no y-axis transform
                                        0.0); //  no z-axis transform
    recordLayer.transform = myRotationTransform;        

这个工作按预期:记录正在铺设好。

为了下一步,使记录旋转,我把这个动画绑定到touchesEnded事件,虽然一旦从测试/学习阶段,这种旋转将不会在用户控制:

CATransform3D currentTransform = recordLayer.transform; // to come back to at the end
CATransform3D myRotationTransform = 
                CATransform3DRotate(currentTransform,
                                    1.0, // go all the way around once
                                    (M_PI_2 * 0.85),    // x-axis change
                                    1.00,    // y-axis change ?
                                    0.0);   // z-axis change ?
recordLayer.transform = myRotationTransform;        
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
myAnimation.duration = 5.0;
myAnimation.fromValue = [NSNumber numberWithFloat:0.0];
myAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
myAnimation.delegate = self;
[recordLayer addAnimation:myAnimation forKey:@"transform.rotation"];

所以我很确定我挂在CATransform3DRotate调用的向量(相信我:我一直在尝试简单的更改的向量来观察更改…现在列出的是简单的最后更改我试过了)。根据我的理解,变换中的x,y和z的值实质上是在动画期间从fromValue到toValue传递的值的百分比。

如果我在正确的轨道理解这一点,是否可能在一个单一的变换表达?或者我必须为每个动画的有效帧,绕z轴旋转原始的直立图像,然后用x轴旋转放置结果?我看到一个问题/答案谈到组合变换,但是这是一个尺度变换后面的旋转变换。我已经搞砸了转换变换,但不是我想我应该得到的(即将一个变换的结果传递给下一个变换参数似乎只是完全执行一个,然后动画另一个)。

如果您使用CATransformLayer作为图像视图图层的父级,这是最简单的。因此,您需要一个使用CATransformLayer作为其图层的自定义视图子类。这是微不足道的:

@interface TransformView : UIView

@end

@implementation TransformView

+ (Class)layerClass {
    return [CATransformLayer class];
}

@end

在您的nib中放置一个TransformView,并添加一个UIImageView作为TransformView的子视图。将这些视图连接到视图控制器中名为transformView和discView的插座。

在视图控制器中,将transformView的变换设置为应用透视图(通过设置m34)和X轴倾斜:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1 / 500.0;
    transform = CATransform3DRotate(transform, .85 * M_PI_2, 1, 0, 0);
    self.transformView.layer.transform = transform;
}

将关键路径transform.rotation.z的动画添加到viewWillAppear中的discView中,并将其删除viewDidDisappear ::

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    animation.duration = 1.0;
    animation.repeatCount = HUGE_VALF;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.discView.layer addAnimation:animation forKey:@"transform.rotation.z"];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.discView.layer removeAllAnimations];
}

结果:

更新

这里有一个Swift操场演示:

import UIKit
import XCPlayground

class TransformView: UIView {
    override class func layerClass() -> AnyClass {
        return CATransformLayer.self
    }
}

let view = UIView(frame: CGRectMake(0, 0, 300, 150))
view.backgroundColor = UIColor.groupTableViewBackgroundColor()
XCPlaygroundPage.currentPage.liveView = view

let transformView = TransformView(frame: view.bounds)
view.addSubview(transformView)

var transform = CATransform3DIdentity
transform.m34 = CGFloat(-1) / transformView.bounds.width
transform = CATransform3DRotate(transform, 0.85 * CGFloat(M_PI_2), 1, 0, 0)
transformView.layer.transform = transform

let image = UIImage(named: "3DgoldRecord")!
let imageView = UIImageView(image: image)
imageView.bounds = CGRectMake(0, 0, 200, 200)
imageView.center = CGPointMake(transformView.bounds.midX, transformView.bounds.midY)
transformView.addSubview(imageView)

let animation = CABasicAnimation(keyPath: "transform.rotation.z")
animation.fromValue = 0
animation.toValue = 2 * M_PI
animation.duration = 1
animation.repeatCount = Float.infinity
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
imageView.layer.addAnimation(animation, forKey: animation.keyPath)

将图像从问题复制到操场资源文件夹,并将其命名为3DgoldRecord.png。结果:

playground result

http://stackoverflow.com/questions/9423892/how-to-rotate-a-flat-object-around-its-center-in-perspective-view

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:ios – 如何在透视图中围绕其中心旋转平面对象?