Search code examples
iosios8parallaxuialertcontroller

Disable parallax iOS in UIAlertController


The app I'm developing only supports portrait mode. However, in iOS8, the now deprecated UIAlertView can go into landscape and displays some other minor problems. In order to fix this, I have decided to give the UIAlertController a try. It works, but I have a new problem which is still a bit annoying. The alert no longer goes into landscape, but it does feature the parallax motion thing. I've added a label and an image to the UIAlertController's view, but those do not feature the parallax thing, meaning it looks pretty weird when users move the phone around.

I'm looking for two options, but I can't find how to do either of them.
The first solution would be to disable the parallax thing, seeing how we don't support it anywhere in the app.
The second solution is to add the parallax support to the label and image.
Anyway, here's the piece of code:

UIAlertController *welcomeAlertController = [UIAlertController
                                              alertControllerWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Would you like to send %@ a message?", @"This message is displayed after adding a new message receiver. The %@ is the name of the receiver."), self.receiver.name]
                                              message:@"\n\n\n\n\n\n\n\n"
                                              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"Skip", @"Skip: meaning user can skip a certain step.")
                               style:UIAlertActionStyleCancel
                               handler:^(UIAlertAction *action)
                               {
                                   [self cancelButtonPressed];
                               }];

UIAlertAction *okAction = [UIAlertAction
                           actionWithTitle:NSLocalizedString(@"Send", @"Send: meaning user can send information to somewhere.")
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction *action)
                           {
                               [self sendButtonClicked];
                           }];

[welcomeAlertController addAction:cancelAction];
[welcomeAlertController addAction:okAction];

smsView.frame = CGRectMake(20, 80, 240, 95);
messageBackgroundView.frame = CGRectMake(0, 0, 240, 80);

warningLabel.frame = CGRectMake(20, 170, 240, 40);
warningLabel.textColor = [UIColor blackColor];

[welcomeAlertController.view addSubview:smsView];  // An UIView
[welcomeAlertController.view addSubview:warningLabel];

[self presentViewController:welcomeAlertController animated:YES completion:nil];

Solution

  • Adding a parallax to the views is deadly simple. It works like this:

    UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    horizontalMotionEffect.minimumRelativeValue = @(-30);
    horizontalMotionEffect.maximumRelativeValue = @(30);
    
    UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    verticalMotionEffect.minimumRelativeValue = @(-30);
    verticalMotionEffect.maximumRelativeValue = @(30);
    
    UIMotionEffectGroup *group = [UIMotionEffectGroup new];
    group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];
    [yourViewHere addMotionEffect:group];
    

    You might want to play with the values @(30) to define the way it all looks.

    And as far as I'm concerned, you can't turn off the parallax on UIAlertController and you might want to implement your own UI copy of that by subclassing a UIView.