为什么UIInterpolatingMotionEffect不会改变UIView的框架?

时间:2014-07-12 17:04:33

标签: ios objective-c uiview parallax

我使用我在另一个问题中找到的这个函数对视图应用了视差效果:

const static CGFloat kCustomIOS7MotionEffectExtent = 10.0;

- (void)applyMotionEffects:(UIView *)YOUR_VIEW
{
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        return;
    }
    UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                                                                    type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    horizontalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
    horizontalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
    UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                                                                  type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    verticalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
    verticalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
    UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
    motionEffectGroup.motionEffects = @[horizontalEffect, verticalEffect];
    [YOUR_VIEW addMotionEffect:motionEffectGroup];
}

它工作正常,但是在记录视图框架后我注意到的是它没有改变。他们是怎么做到的?有什么特别的原因吗?这使得继续开发变得容易得多,但它没有意义,因为实际的视图位置随着视差效应的发生而变化。

1 个答案:

答案 0 :(得分:3)

我认为这是因为与大多数动画一样,UIInterpolatingMotionEffect使用视图presentationLayer(模型图层)的layer来制作动画。一旦动画实际完成,属性通常应用于模型层(在这种情况下,完成实际上只是在运动效果结束时将其放回到起始位置,因此实际模型层永远不会更改)。

尝试检查表示层的属性。

CALayer *presentationLayer = [myView.layer presentationLayer];

CGRect frame = presentationLayer.frame;
CGPoint position = presentationLayer.position;
相关问题