简体   繁体   English

tvOS中出现意外的运动效应振荡

[英]Unexpected motion effect oscillations in tvOS

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]  
    }
}

resulting in the following behavior both in Simulator and on the new Apple TV 在Simulator和新Apple TV上导致以下行为

Is there a way to avoid the oscillations? 有没有办法避免振荡?

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. 设备似乎一次产生两个倾斜。

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. 最好的解决方案可能是直接使用Core Motion并完全避免本机转换。 Or use it with smaller distances because then the oscillation won't be visible. 或者使用较小的距离,因为振动将不可见。

I tried your code too. 我也尝试了你的代码。 This is not a problem about the simulator. 这不是关于模拟器的问题。 The problem comes only when you change the touch direction too fast. 只有在您更快地更改触摸方向时才会出现问题。 The UIInterpolatingMotionEffect starts a new motion before finishing to interpolate the previous one. UIInterpolatingMotionEffect 完成插入前一个动作之前启动一个新动作。 So there happen to be two motions acting on your view concurrently. 所以碰巧有两个动作同时对你的观点起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM