我对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.com/example-code/uikit/how-to-create-a-parallax-effect-in-uikit,但是我的imageView仍然是静态的。
我使用的是Xcode 11,iOS 13。
发布于 2020-05-22 05:59:06
在您共享的代码中,我注意到的一件事是,您在枚举T和.TiltAlongVerticalAxis中使用了大写的->,它们不编译(至少在Xcode 11.4.1上)需要将其修改为小写T的->。
苹果为这些企业提供的文档是这里。
除此之外,我不认为您共享的代码有任何问题。实际上,我在视图中创建了一个带有简单imageView的示例项目,在其中添加了约束,这样它就填满了它的superview的整个空间。
然后我添加了您共享的运动效果代码,并在一个实际的设备(iPad Pro)上运行它。效果很好。
你能检查一些可能是问题根源的东西吗?
Settings > Accessibility > Motion > Reduce Motion是否打开。因为如果打开它,那么UIInterpolatingMotionEffect就不能工作了。imageView /viewDidLayout中)编辑该代码的框架,则不应该这样做。在任何情况下,如果这些东西都没有帮助您解决问题,您能否创建一个简单的示例项目来演示该bug并将其发布到Github上?如果我真的能重现这个问题的话,我可以试着帮忙。
https://stackoverflow.com/questions/61927204
复制相似问题