UIView に視差効果 (Parallax) を追加する

iOS 7 の壁紙で一躍有名になった視差効果 (パララックス) は、手軽に組み込むことができます。追加したい UIView に UIInterpolatingMotionEffect を加えるだけです。

次の例では、view に視差効果を追加しています。

let effectX: UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: UIInterpolatingMotionEffectType.TiltAlongHorizontalAxis)
let effectY: UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: UIInterpolatingMotionEffectType.TiltAlongVerticalAxis)
effectX.maximumRelativeValue = 25
effectX.minimumRelativeValue = -25
effectY.maximumRelativeValue = 25
effectY.minimumRelativeValue = -25
view.addMotionEffect(effectX)
view.addMotionEffect(effectY)

maximumRelativeValueminimumRelativeValue の値で視差効果の大きさを調整できるので、複数の UIView を異なった値にする事で奥行きを表現できます。

作成者: コネタねこ

上から読んでも『コネタねこ』、下から読んでも『コネタねこ』。贈り物に悩んだら、コネタねこの小ネタ帳。欲しくなっちゃうプレゼント、そんなアイデアのぞきにきてね♪

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です