问题描述
我正在使用以下代码进行运动效果振荡
I am experiencing motion effect oscillations using the following code
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.rootViewController = ExampleController()
self.window!.makeKeyAndVisible()
return true
}
}
class ExampleController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let redView = UIView(frame: CGRect(x: 200, y: 200,
width: 800, height: 500))
redView.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(redView)
let effect = UIInterpolatingMotionEffect(keyPath: "center.x",
type: .TiltAlongHorizontalAxis)
effect.minimumRelativeValue = -100
effect.maximumRelativeValue = 100
let effectGroup = UIMotionEffectGroup()
effectGroup.motionEffects = [effect]
redView.motionEffects = [effectGroup]
}
}
在模拟器和新的Apple TV上都会导致以下行为
resulting in the following behavior both in Simulator and on the new Apple TV
有办法避免振荡吗?
推荐答案
我已经在模拟器中尝试了您的代码,并打印了水平偏移量:
I have tried your code in the simulator, printing the horizontal offset:
public class MyMotionEffect : UIInterpolatingMotionEffect {
public override func keyPathsAndRelativeValuesForViewerOffset(viewerOffset: UIOffset) -> [String : AnyObject]? {
print("\(viewerOffset.horizontal)")
return super.keyPathsAndRelativeValuesForViewerOffset(viewerOffset);
}
}
一段时间后,我可以重现振荡.产生的(水平)偏移量具有以下图表:
After a while I could reproduce the oscillation. The resulting (horizontal) offset has the following chart:
您可以看到在倾斜中已经存在振荡.该设备似乎一次产生了两个倾斜.
You can see the oscillation is already present in the tilt. It seems the device is generating two tilts at once.
一种解决方案是添加启发式方法(将偏移值与前两个值进行比较,并忽略是否有符号变化,或者忽略具有突然变化的偏移.不幸的是,这两种解决方案都不完美).最好的解决方案可能是直接使用Core Motion并完全避免本机转换.或以较小的距离使用它,因为这样振荡将不可见.
One solution is to add heuristics (compare offset value with two previous and ignore if there was a sign change, or ignore offsets with abrupt change. Neither solution is perfect unfortunately).The best solution would probably be to use Core Motion directly and avoid the native transformations entirely. Or use it with smaller distances because then the oscillation won't be visible.
这篇关于tvOS中的意外运动效应振荡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!