在OpenGL ES中使用UIMotionEffect [英] Use UIMotionEffect with OpenGL ES

查看:121
本文介绍了在OpenGL ES中使用UIMotionEffect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我专门为我的应用使用OpenGL ES(2.0)。我正在尝试使用 UIMotionEffect 来处理我的一个OpenGL对象 - 我需要的只是倾斜值。

I'm using OpenGL ES (2.0) exclusively for my app. I'm trying to get UIMotionEffect to work with one of my OpenGL objects - where all I need are the "tilt" values.

UIInterpolatingMotionEffect 只能应用于 UIView ,黑客就是将 UIInterpolatingMotionEffect 应用到 UIView 并从每帧中获取值,同时不渲染假设 UIView 。但是它似乎是唯一的解决方案。

UIInterpolatingMotionEffect can only be applied to a UIView, and a hack would be to apply UIInterpolatingMotionEffect to a UIView and grab the values from there per frame, while not rendering the supposed UIView. But it seems far too hack-ish to be the only solution.

我试图继承 UIMotionEffect ,但不能弄清楚如何 - (NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset 工作(即调用它以及如何检索我需要的值)。

I tried to subclass UIMotionEffect, but couldn't figure out how - (NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset worked (ie. what calls it and how to retrieve the values I need).

关于如何在OpenGL中使用 UIMotionEffect 的任何想法?

Any ideas on how could I use UIMotionEffect with OpenGL?

推荐答案

我终于测试了@rickster建议的方法:connect UIInterpolatingMotionEffect 到我自己定制的GLKView / GL View传递键并且只是检索他们。不幸的是,这个想法很糟糕。

I finally tested the method suggested by @rickster: connect UIInterpolatingMotionEffect to my GLKView/GL View pass keys that are custom made and just retrieve them. Unfortunately, that idea royally fails.

首先,传递给 UIInterpolatingMotionEffect 的密钥必须是 UIView 动画属性,不能是自定义变量/属性。考虑到大多数人不希望他们的整个上下文在单向方向/强度上出现视差,这种方法会失败。

First, the keys that are passed to UIInterpolatingMotionEffect must be UIView animatable properties, and can't be custom variables/properties. Considering most people wouldn't want their entire context to parallax in one way direction/intensity, this method will fail.

其次,你不能只检索这些值。如果您将 center.x 作为键传递, UIInterpolatingMotionEffect 将不会更新 center.x 因为这些更改被视为动画。因此,您可以检索动画 center.x ,例如 [[view.layer presentationLayer] center];

Second, you can't just retrieve the values. If you pass center.x as a key, UIInterpolatingMotionEffect won't update center.x since the changes are considered "animations". As such, you have you retrieve the animated center.x, for example [[view.layer presentationLayer] center];

最终注释:只需在主线程上创建一个单独的 UIView ,将其添加到 UIViewController 并系统地从中检索值(也在主线程上)。到目前为止,我发现这是最简单的解决方案。

Final Notes: Just create a separate UIView on the main thread, add it to your UIViewController and systematically retrieve values from it (also on the main thread). I found that to be the simplest solution by far.

此外,由于我需要在单独的线程上检索值,因此我通常会有1个值,因为我调度到主线程以检索当前值,同时检索最后一个值集(确保该变量是线程安全的/原子的)。

Also, since I needed to retrieve the values on a separate thread, I'm generally 1 value behind, as I dispatch to the main thread to retrieve the current value, while at the same time retrieving the last value set (make sure the variable is thread-safe/atomic).

编辑

由于最好在主线程上访问presentationLayer,但我的方法被调用为非主线程,我发送检索值并将其放在原子属性中的主线程。然后我会在线程外返回原子属性。这确保我的辅助线程不会被主线程阻塞,我可以检索我需要的值。

Since its best to access the presentationLayer on the main thread, but my method is called for a thread that isn't main, I dispatch the main thread to retrieve the value and place it in an atomic property. I would then return the atomic property outside the thread. This makes sure my secondary thread doesn't get blocked by the main thread, and I can retrieve the values I need.

这是我用来从a检索值的代码不是主线程的线程:

Here's the code I used to retrieve values from a thread that isn't the main thread:

@property (atomic) GLKVector2 currentOffset;
@property (atomic, readonly) GLKVector2 offset;

- (GLKVector2)offset
{
    dispatch_async(dispatch_get_main_queue(), ^{
        CGRect frame = [[self.view.layer presentationLayer] frame];
        self.currentOffset = (GLKVector2){-frame.origin.x, -frame.origin.y};
    });

    return self.currentOffset;
}

这篇关于在OpenGL ES中使用UIMotionEffect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆