在iPhone上使用CoreMotion/DeviceMotion模拟图像浮动效果

发布于 2024-10-22 04:21:34 字数 1082 浏览 0 评论 0原文

我在 ViewController 中有一系列图像。

我通过转换它们的各个图层来“浮动”它们:

img.layer.transform = CATransform3DMakeTranslation(0.0f, 0.0f, myZ);

这给出了使图层浮动在屏幕上方的效果 - 在模拟器中,不会有可见的效果(这是正确的)。

我想要做的是产生这样的效果:向左/向右/向前/或向后移动设备会使其看起来微妙地像是浮动的。当您向左倾斜设备时,整个视图应该向右倾斜。这会让你感觉移动设备可以让你看到角落周围——也就是说,它会给人一种图像确实漂浮在屏幕上方的感觉,因为它们会以不同的速率移动(基于它们的 z 索引)。

我制作了一个测试项目(此处的项目文件),其中有一个演示此项目的示例项目。

我的问题是我不是数学家,所以我正在努力寻找模拟微妙浮动效果的最佳方法。现在,我有一个 DeviceMotion 的侦听器,然后它会执行以下操作:

self.view.layer.sublayerTransform = CATransform3DMakeRotation(20.0f * M_PI / 180.0f, 2*motion.attitude.pitch, -2*motion.attitude.roll, 0);

这非常接近我想要的,但并不完全正确。

我认为这种效果可以用于许多不同的应用程序。我希望将其扩展到朋友和我正在研究的面部检测方面(这样它就会根据人脸的运动来移动父母的视图 - 即使他们保持手机/设备完全静止)。

我意识到我会让人们回答“只使用 OpenGL”。这不是我需要的答案 - 除非您发布一段代码来展示如何将其集成到该项目中。 (我不是在寻找要解决的新问题。:-)

再说一遍,完整的项目在这里(iphone 浮动视图)对于任何想要看到现在效果的人来说。 (当它起作用时,我将把完整的(工作)项目链接到这里供后代使用。)

I have a series of images within a ViewController.

I am "floating" them by transforming their individual layers:

img.layer.transform = CATransform3DMakeTranslation(0.0f, 0.0f, myZ);

This gives the effect of making the layers float above the screen -- in the Simulator, there would be no visible effect (which is correct).

What I want to do is have an effect where moving a device left/right/forward/or back would make it look subtly like the layers are floating. When you tilt the device to the left, it should angle the entire view to the right. This will make it feel like moving the device lets you see around corners -- i.e. it will give a feeling that the images really are floating above the screen, because they would move at different rates (based on their z-index).

I have made a test project (project file here) which has a sample project that demos this.

My problem is that I am not a math person, so I am struggling with the best way to simulate the subtle floating effect. Right now, I've got a listener for DeviceMotion, which then does:

self.view.layer.sublayerTransform = CATransform3DMakeRotation(20.0f * M_PI / 180.0f, 2*motion.attitude.pitch, -2*motion.attitude.roll, 0);

This is very close to what I want, but it's not exactly right.

I think this effect will be something that could be used in a number of different applications. I am hoping to extend this to something a friend and I are working on with face detection (so it would move the parent view based on the movement of a person's face -- even while they keep the phone/device perfectly still).

I realize I will get people answering "just use OpenGL". That's not the answer I need -- unless you post a chunk of code that shows how to integrate it within this project. (I'm not looking for new problems to solve. :-)

Again, the full project is here (iphone floating views) for anyone who would like to see the effect as it is right now. (When this is working, I will leave the full (working) project linked here for posterity.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-10-29 04:21:34

我想我已经得到了这个!使用这个:

    [appDelegate.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                                   withHandler:
     ^(CMDeviceMotion *motion, NSError *error) {

         CATransform3D transform;
         transform = CATransform3DMakeRotation(motion.attitude.pitch, 1, 0, 0);
         transform = CATransform3DRotate(transform, motion.attitude.roll, 0, 1, 0);

         self.view.layer.sublayerTransform = transform;
     }];

对我来说就像魅力一样,如果你想翻转轴,只需在那些 1 前面添加减号即可。

I think I've got this! Use this:

    [appDelegate.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                                   withHandler:
     ^(CMDeviceMotion *motion, NSError *error) {

         CATransform3D transform;
         transform = CATransform3DMakeRotation(motion.attitude.pitch, 1, 0, 0);
         transform = CATransform3DRotate(transform, motion.attitude.roll, 0, 1, 0);

         self.view.layer.sublayerTransform = transform;
     }];

Works like charm for me, if you want to flip axes, just add minus sign in front of those 1s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文