在iPhone上拖动UIScrollView时,OpenGL ES视图中的动画会冻结 [英] Animation in OpenGL ES view freezes when UIScrollView is dragged on iPhone

查看:149
本文介绍了在iPhone上拖动UIScrollView时,OpenGL ES视图中的动画会冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动画透明的OpenGL ES子视图(Apple的模板EAGLView类的修改),它绘制一个旋转的球体。就像Apple的例子一样,CADisplayLink用在可用的设备上。

I have an animated transparent OpenGL ES subview (a modification of Apple's template EAGLView class) which draws a rotating sphere. Just like Apple's example, CADisplayLink is used on devices where available.

在同一个屏幕上,有一个UIScrollView包含可以选择的UIButtons。当用户滚动UIScrollView时,我的EAGLView动画会冻结。此行为在iOS模拟器4.2和iPhone 2G设备上的iPhone OS 3.1.3上重现。

On the same screen, there is a UIScrollView containing UIButtons that can be selected. When the user scrolls the UIScrollView, the animation of my EAGLView freezes. This behavior is reproduced on iOS Simulator 4.2 and on iPhone OS 3.1.3 on an iPhone 2G device.

有关如何防止EAGLView暂停的任何想法,分开编码我自己的滚动视图?

Any ideas on what to do to prevent pause of the EAGLView, apart from coding my own scroll view?

推荐答案

滚动期间是否 CADisplayLink 触发取决于您将其添加到运行循环的模式。可能你有这个,某个地方:

Whether CADisplayLink fires during scrolls depends on the mode with which you add it to the run loop. Probably you have this, somewhere:

[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

UIApplication添加了一个运行循环模式, UITrackingRunLoopMode ,用于跟踪控件,包括滚动视图滚动时。因此,此时runloop将退出默认模式,因此您的显示链接(以及任何计时器, NSURLConnections 等,在默认模式下添加)将无法直到恢复默认模式为止。

UIApplication adds a run loop mode, UITrackingRunLoopMode, for 'tracking in controls', which includes when a scrollview is scrolling. So at that point the runloop is switched out of the default mode and hence your display link (and also any timers, NSURLConnections, etc, added in the default mode) will fail to fire until default mode is restored.

快速修复:将您的代码更改为:

Quick fix: change your code to:

[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

UITrackingRunLoopMode被认为是常见模式之一。

UITrackingRunLoopMode is considered one of the common modes.

花费太多时间打断UIKit会导致控制响应能力非常差,所以你需要小心。它是大规模地偏离主题,但是虽然OpenGL是模态的,因此不是特别的线程友好,你可以使用EAGLSharegroup在单独的线程上进行渲染,然后将其推送到主线程。

Spending too much time interrupting UIKit can lead to some very poor control responsiveness, so you need to be careful. It'd be to diverge from the topic massively, but although OpenGL is modal and therefore not particularly threading friendly, you can use an EAGLSharegroup to do rendering on a separate thread and then push it onto the main thread.

(2016)Swift3中的一个例子......

An example in (2016) Swift3...

let d = CADisplayLink(target: self, selector: #selector(ThisClassName.updateAlpha))
d.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)


//and then, for example...
func updateAlpha() {
  let a = leader.layer.presentation()?.value(forKey: "opacity") as! CGFloat
  follower.alpha = a
  }

这篇关于在iPhone上拖动UIScrollView时,OpenGL ES视图中的动画会冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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