![](/img/trans.png)
[英]Looking for an “Zoom”-effect event handler for UICollectionViewCell in tvOS
[英]Unexpected motion effect oscillations in 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]
}
}
在Simulator和新Apple TV上導致以下行為
有沒有辦法避免振盪?
我在模擬器中嘗試了你的代碼,打印水平偏移:
public class MyMotionEffect : UIInterpolatingMotionEffect {
public override func keyPathsAndRelativeValuesForViewerOffset(viewerOffset: UIOffset) -> [String : AnyObject]? {
print("\(viewerOffset.horizontal)")
return super.keyPathsAndRelativeValuesForViewerOffset(viewerOffset);
}
}
過了一會兒,我可以重現振盪。 生成的(水平)偏移量具有以下圖表:
您可以看到傾斜中已經存在振盪。 設備似乎一次產生兩個傾斜。
一種解決方案是添加啟發式方法(將偏移值與前兩個進行比較並忽略是否存在符號更改,或忽略具有突然更改的偏移。不幸的是,這兩種解決方案都不完美)。 最好的解決方案可能是直接使用Core Motion並完全避免本機轉換。 或者使用較小的距離,因為振動將不可見。
我也嘗試了你的代碼。 這不是關於模擬器的問題。 只有在您更快地更改觸摸方向時才會出現問題。 UIInterpolatingMotionEffect
在完成插入前一個動作之前啟動一個新動作。 所以碰巧有兩個動作同時對你的觀點起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.