8

我最初是在寻找这个问题的答案,但在我问这个问题之前,我想到了解决方案,并且效果很好。

基本上,我的问题是,在 iOS 7 下,你可以有一个随视差移动的阴影层吗?答案是肯定的,这就是你如何做到的;

- (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];
}

不是特别难。

4

0 回答 0