只需显示UIInterpolatingMotionEffect的值?

时间:2014-09-12 14:25:08

标签: ios uiview calayer key-value-observing uimotion

这是一个谜题,想象一下典型的UIInterpolatingMotionEffect ,,,

UIInterpolatingMotionEffect *horizontalMotionEffect =
  [[UIInterpolatingMotionEffect alloc]
    initWithKeyPath:@" .. some property .."
     type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-50);
horizontalMotionEffect.maximumRelativeValue = @(50);
[someView addMotionEffect:horizontalMotionEffect];

通常情况下,你必须在第3行输入一个属性 - 比如,center.x

但是 - 如果我(非常简单地)希望看到UIInterpolatingMotionEffect的值,该怎么办?

(或者我可能会将它用于其他目的,比如说。)

我是否真的需要继承CALayer并创建一个新的可动画属性?!

访问该值似乎非常复杂。我能想到的唯一技巧就是制作一个不可见的视图,将其设置为中心,然后访问该值!虽然看起来很傻。

有什么想法吗?

1 个答案:

答案 0 :(得分:7)

您可以继承UIInterpolatingMotionEffect并挂钩方法-(NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset以记录viewerOffset以获取规范化值或查看超级实现返回的字典以获取内插值之间的值你的最低和最高。

UILogInterpolatingMotionEffect.h

@interface UILogInterpolatingMotionEffect : UIInterpolatingMotionEffect

@end

UILogInterpolatingMotionEffect.m

@implementation UILogInterpolatingMotionEffect

-(NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset
{
    NSDictionary *superDictionary = [super keyPathsAndRelativeValuesForViewerOffset:viewerOffset)];
    NSLog(@"%@, %@", NSStringFromUIOffset(viewerOffset), superDictionary);
    return superDictionary;
}

@end

PS:如果你只对viewerOffset感兴趣,你甚至可以继承UIMotionEffect。另请参阅UIMotionEffect class reference