Reputation: 145
My parallax effect for UIView
is not working at all. I need something like a moving background image on iPhone tilt (pseudo-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)
I've tried to use this code. I've read https://www.hackingwithswift.com/example-code/uikit/how-to-create-a-parallax-effect-in-uikit but my imageView
remains static.
I'm using Xcode 11, iOS 13.
Upvotes: 3
Views: 917
Reputation: 5448
In the code that you have shared, one thing that I noticed is that you are using capital T
in the enums .TiltAlongHorizontalAxis
and .TiltAlongVerticalAxis
, which don't compile (at least on Xcode 11.4.1) -> those need to be changed to lowercase T
.
Apple's documentation for those enums is here.
Other than that, I don't see any problems with the code that you shared. In fact I created a sample project with a simple imageView
in the view to which I added constraints so that it fills up the entire space of its superview.
I then added the motion effects code that you shared, and ran it on an actual device (iPad Pro). It worked perfectly.
Could you check a few things that might be a source of the problem:
Settings
> Accessibility
> Motion
> Reduce Motion
is not turned on. Because if it is turned on, then UIInterpolatingMotionEffect
won't work.imageView
in code anywhere (or especially in viewWillLayout
/ viewDidLayout
), you shouldn't be doing that.In any case, if none of these things help you to fix your problem, can you create a simple sample project which demonstrates the bug and post it on Github? I can try to help if I can actually reproduce the problem.
Upvotes: 2