如何在swift中实现动态背景图像

SGo*_*win 4 image dynamic ios swift

我想实现与"真实运动背景"的起始页面相同的行为.当您倾斜手机时,背景图像会移动一点,使其看起来像3D并且"真实".

这是我在我的应用程序开始页面中想要的.

我该怎么做?

Rob*_*Rob 6

UIInterpolatingMotionEffect.

您可以为每个轴创建一个,创建一个组,并将动作效果组添加到UIImageView后台.

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)
Run Code Online (Sandbox Code Playgroud)

因此,当您围绕y轴倾斜时,图像将向左和向右移动.当您围绕x轴倾斜时,图像将上下移动.

来自developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/motion_event_basics/motion_event_basics.html#//apple_ref/doc/uid/TP40009541-CH6-SW14 (图片取自iOS事件处理指南.)