How to implement dynamic background image in swift
See UIInterpolatingMotionEffect
.
You can create one for each axis, create a group, and add the motion effect group to the UIImageView
in the background.
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)
Thus, as you tilt it about the y-axis, the image will move left and right. As you tilt about the x-axis, the image will move up and down.
(Image taken from the Event Handling Guide for iOS.)