SGoldwin SGoldwin - 1 year ago 72
Swift Question

How to implement dynamic background image in swift

I would like to implement the same behaviour that the start page with "true motion backgrounds". When you tilt the phone the background image move a bit which makes it look 3D and "real".

This what I would like in my application start page.

How do I make this?

Rob Rob
Answer Source

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]


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.

from (Image taken from the Event Handling Guide for iOS.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download