Unexpected motion effect oscillations in tvOS

半腔热情 提交于 2019-12-04 10:11:49

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. 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. So there happen to be two motions acting on your view concurrently.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!