And*_*rei 5 swift uimotioneffect tvos
我正在使用以下代码体验运动效果振荡
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]
}
}
Run Code Online (Sandbox Code Playgroud)
在Simulator和新Apple TV上导致以下行为

有没有办法避免振荡?
我已经在模拟器中尝试了您的代码,打印水平偏移:
public class MyMotionEffect : UIInterpolatingMotionEffect {
public override func keyPathsAndRelativeValuesForViewerOffset(viewerOffset: UIOffset) -> [String : AnyObject]? {
print("\(viewerOffset.horizontal)")
return super.keyPathsAndRelativeValuesForViewerOffset(viewerOffset);
}
}
Run Code Online (Sandbox Code Playgroud)
一段时间后我可以重现振荡。生成的(水平)偏移量如下图所示:
您可以看到倾斜中已经存在振荡。该设备似乎同时产生两个倾斜。
一种解决方案是添加启发式方法(将偏移值与前两个值进行比较,并忽略是否存在符号变化,或者忽略突然变化的偏移量。不幸的是,这两种解决方案都不是完美的)。最好的解决方案可能是直接使用 Core Motion 并完全避免本机转换。或者在较小的距离下使用它,因为这样振荡就不会可见。
| 归档时间: |
|
| 查看次数: |
309 次 |
| 最近记录: |