8

I was originally seeking the answer to this question, but before I could ask it, the solution occurred to me, and it works perfectly.

Basically, my question way, under iOS 7 can you have a shadow layer that moves with parallax? The answer is yes, and here is how you do it;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.layer.masksToBounds = NO;
    self.view.layer.cornerRadius = 2; // if you like rounded corners
    self.view.layer.shadowOffset = CGSizeMake(1, 1);
    self.view.layer.shadowRadius = 2;
    self.view.layer.shadowOpacity = 0.5;
    UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.shadowOffset.height" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    verticalMotionEffect.minimumRelativeValue = @(20);
    verticalMotionEffect.maximumRelativeValue = @(-20);

    UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.shadowOffset.width" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    horizontalMotionEffect.minimumRelativeValue = @(20);
    horizontalMotionEffect.maximumRelativeValue = @(-20);

    UIMotionEffectGroup *group = [UIMotionEffectGroup new];
    group.motionEffects = @[horizontalMotionEffect, verticalMotionEffect];

    [self.view addMotionEffect:group];
}

Not particularly difficult.

Owen Godfrey
  • 3,361
  • 2
  • 22
  • 18

0 Answers0