首页 > 解决方案 > 如何在 SwiftUI 中使用 UIMotionEffect?

问题描述

知道如何在 SwiftUI 中使用 UIMotionEffect 吗?我在想可能有一种方法可以将运动效果连接到状态变量,然后在 SwiftUI 中使用该变量,我们会得到与 UIKit 类似的结果。

简单的 UIKit 示例

let value = 12

let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "layer.transform.translation.y", type: .tiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = value
verticalMotionEffect.maximumRelativeValue = -value

let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "layer.transform.translation.x", type: .tiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = value
horizontalMotionEffect.maximumRelativeValue = -value

let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
view.addMotionEffect(group)

标签: swiftuiuimotioneffect

解决方案


推荐阅读