Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 parallax effect in my view controller

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.)

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?

like image 826
Dan Fabulich Avatar asked Sep 24 '13 04:09

Dan Fabulich


People also ask

Does iOS still have parallax?

Parallax is a new feature that is introduced in iOS 7. As the user moves their device, the background image and icons move on the screen to match the movement of the device. Parallax gives the device a quasi-3D effect and adds depth to it, but some people prefer to use their iPhone, iPad, or iPod touch sans the motion.

How do I turn off parallax on my iPhone?

Switching off Parallax on your iPhone is pretty easy: You can do it from your “Settings” app. Locate the section labeled “Accessibility”, tap Motion and enable the toggle to the right of “Reduce Motion” toggle. This will disable Parallax and a number of other minor animations and visual effects.

What is parallax in iOS?

Parallax effect where your wallpaper, apps, and alerts that move or shift slightly as you tilt your device are disabled. Animation and effects in certain apps are disabled.


2 Answers

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 UIInterpolatingMotionEffect, as explained 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 (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) 

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).
like image 122
veducm Avatar answered Sep 26 '22 16:09

veducm


Translated to swift in case anyone is lazy. Please vote @veducm answer up if you found this useful

@IBOutlet var background : UIImageView!  func parallaxEffectOnBackground() {     let relativeMotionValue = 50     var verticalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y",         type: .TiltAlongVerticalAxis)     verticalMotionEffect.minimumRelativeValue = -relativeMotionValue     verticalMotionEffect.maximumRelativeValue = relativeMotionValue      var horizontalMotionEffect : UIInterpolatingMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x",         type: .TiltAlongHorizontalAxis)     horizontalMotionEffect.minimumRelativeValue = -relativeMotionValue     horizontalMotionEffect.maximumRelativeValue = relativeMotionValue      var group : UIMotionEffectGroup = UIMotionEffectGroup()     group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]      self.background.addMotionEffect(group) } 
like image 33
Lucas Avatar answered Sep 24 '22 16:09

Lucas