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

标签 ios objective-c uiview parallax

我使用在另一个问题中找到的这个函数将视差效果应用于 View :

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

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

最佳答案

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

尝试检查表示层的属性。

CALayer *presentationLayer = [myView.layer presentationLayer];

CGRect frame = presentationLayer.frame;
CGPoint position = presentationLayer.position;

关于ios - 为什么 UIInterpolatingMotionEffect 不改变 UIView 的框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24715330/

相关文章:

ios - UITextField - UIView 以编程方式调整大小

c++ - Xcode:为什么仅在静态库中重命名为 .mm 失败并出现 undefined symbol "___gxx_personality_sj0"?

objective-c - 在运行时替换SQLite数据库

iphone - 检测 UIView 子类中的自转

ios - 使用后台线程更新界面的更快方法

ios - 删除除命名之外的所有 subview

ios - Apple 证书对 PKCS7_verify 无效

ios - UISearchBar:更改输入字段的背景颜色

iOS 将服务器时间转换为设备本地时间?

ios - UIStatusBar 不会消失。悲伤和沮丧随之而来