Question

thanks for turning up :-)

I noticed that on the iPad the Form Sheet view does not have any iOS 7 parallax effects, and I would actually like to incorporate this because I think it kinda looks cool.

No parallex effect *sad face*

Well this is what I have and it doesn't have that, so is there a way to do this? How would one go about doing it? I've googled and googled and google has finally failed me because nothing appropriate is appearing.

Thanks :)

Was it helpful?

Solution

This can be done with some effort.

First off, you need to know how to add parallax to a view in general:

I have the following category method for UIView to make it easy:

- (void)addDepthMotionX:(CGFloat)x y:(CGFloat)y {
    Class clazz = NSClassFromString(@"UIInterpolatingMotionEffect");
    if (clazz) {
        BOOL reverse = NO;
        if (CGAffineTransformEqualToTransform(self.transform, CGAffineTransformMakeRotation(-M_PI_2)) || CGAffineTransformEqualToTransform(self.transform, CGAffineTransformMakeRotation(M_PI_2))) {
            reverse = YES;
        }

        UIInterpolatingMotionEffect *eff = [[clazz alloc] initWithKeyPath:@"center.x" type:reverse ? UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis : UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
        eff.maximumRelativeValue = @(x);
        eff.minimumRelativeValue = @(-x);
        [self addMotionEffect:eff];
        eff = [[clazz alloc] initWithKeyPath:@"center.y" type:reverse ? UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis : UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
        eff.maximumRelativeValue = @(y);
        eff.minimumRelativeValue = @(-y);
        [self addMotionEffect:eff];
    }
}

The code is guarded so it won't crash if called from other than iOS 7 (or later).

Now to add parallax to a view you simply do:

[someView addDepthMotionX:10 y:10]; // pick an appropriate depth value

The final step to your question is to apply this to the root of the view controller you are displaying.

Here's code you can add in the viewWillAppear: method of the view controller. Adjust to meet your needs:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (self.isBeingPresented || self.isMovingToParentViewController) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && self.modalPresentationStyle == UIModalPresentationFormSheet) {
            [self.navigationController.view.superview addDepthMotionX:15 y:15];
        }
    }
}

One issue with this code is if the user rotates the iPad 90 degrees after the form sheet is presented, the parallax effect needs to be updated. But this will get you started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top