我的视差效果 UIView
完全不能用。我需要一些类似iPhone倾斜的移动背景图片(伪3D)。
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -50
horizontalMotionEffect.maximumRelativeValue = 50
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -50
verticalMotionEffect.maximumRelativeValue = 50
let motionEffectGroup = UIMotionEffectGroup()
motionEffectGroup.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
imageView.addMotionEffect(motionEffectGroup)
我已经尝试使用这段代码。我读过 https:/www.hackingwithswift.comexample-codeuikithow-to-create-a-parallax-effect-in-uikit 但我 imageView
仍然是静态的。
我使用的是Xcode 11,iOS 13。
在你分享的代码中,我注意到一件事,就是你使用了大写的 T
在枚举中 .TiltAlongHorizontalAxis
和 .TiltAlongVerticalAxis
,不能编译(至少在Xcode 11.4.1上)->;这些需要改成小写。T
.
苹果公司关于这些枚举的文档是 此处.
除此之外,我看不出你分享的代码有什么问题。事实上,我创建了一个示例项目,其中有一个简单的 imageView
的视图中,我为其添加了约束条件,使其填满了整个superview的空间。
然后,我添加了你分享的运动效果代码,并在实际设备(iPad Pro)上运行它。它工作得很完美。
你能不能检查一下可能是问题来源的几件事。
Settings
> Accessibility
> Motion
&gt。Reduce Motion
是没有打开的。因为如果它被打开,那么 UIInterpolatingMotionEffect
不会工作。imageView
在任何地方的代码中(尤其是在 viewWillLayout
viewDidLayout
),你不应该这样做。在任何情况下,如果这些事情都不能帮助你解决你的问题,你能不能创建一个简单的示例项目来演示这个错误,并将其发布到Github上?如果我能够真正重现这个问题,我可以尝试帮助你。