我的视图控制器中的iOS 7视差效果 [英] iOS 7 parallax effect in my view controller

查看:69
本文介绍了我的视图控制器中的iOS 7视差效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Objective-C开发iOS 7应用程序。我的应用程序中有一个屏幕,带有几个按钮和漂亮的背景图像。 (这是一个简单的xib, UIButtons UIImageView 之上。)

I'm developing an app for iOS 7 in Objective-C. I've got a screen in my app with a few buttons and a pretty background image. (It's a simple xib with UIButtons on top of a UIImageView.)

我认为如果这些按钮具有iOS 7主屏幕的视差效果会很酷,所以如果你倾斜手机就可以看到背景。

I was thinking that it'd be cool if those buttons had the parallax effect that the iOS 7 home screen has, so if you tilt the phone you could see the background.

如何在我自己的应用程序中实现该效果?

How can I implement that effect in my own app?

推荐答案

在iOS 7中,Apple推出了 UIMotionEffect 添加与用户设备方向相关的动作效果。例如,要模拟主屏幕上的视差效果,您可以使用子类 UIInterpolationMotionEffect ,如此处此处,只需几行代码。

With iOS 7, Apple introduced UIMotionEffect to add Motion effects that are related to the orientation of the user’s device. For example, to emulate the parallax effect on the home screen, you can use the subclass UIInterpolationMotionEffect, as explained here and here, just with a few lines of code.

Objective-C

// Set vertical effect
UIInterpolatingMotionEffect *verticalMotionEffect = 
  [[UIInterpolatingMotionEffect alloc] 
  initWithKeyPath:@"center.y"
             type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
verticalMotionEffect.minimumRelativeValue = @(-10);
verticalMotionEffect.maximumRelativeValue = @(10);

// Set horizontal effect 
UIInterpolatingMotionEffect *horizontalMotionEffect = 
  [[UIInterpolatingMotionEffect alloc] 
  initWithKeyPath:@"center.x"     
             type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-10);
horizontalMotionEffect.maximumRelativeValue = @(10);

// Create group to combine both
UIMotionEffectGroup *group = [UIMotionEffectGroup new];
group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];

// Add both effects to your view
[myBackgroundView addMotionEffect:group];

Swift (感谢@Lucas):

Swift (Thanks to @Lucas):

// Set vertical effect
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",
type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -10
verticalMotionEffect.maximumRelativeValue = 10

// Set horizontal effect
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",
    type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -10
horizontalMotionEffect.maximumRelativeValue = 10

// Create group to combine both
let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]

// Add both effects to your view
myBackgroundView.addMotionEffect(group)

此外,您可以找到一堆库来更轻松地执行此操作或将此功能添加到较旧的iOS版本中离子:

Also, you can find a bunch of libraries to do this easier or to add this functionality to older iOS versions:

  • NGAParallaxMotion (requires iOS 7).
  • DVParallaxView (requires iOS 5.0 or higher and ARC).
  • MKParallaxView (tested with iOS 6.0, requires ARC).
  • UIView-MWParallax (tested with iOS 6.1, requires ARC).

这篇关于我的视图控制器中的iOS 7视差效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆