只需显示UIInterpolatingMotionEffect的值?

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

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

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

但是 – 如果我(非常简单地)想要看到UIInterpolatingMotionEffect的值,该怎么办?

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

我是否真的必须inheritanceCALayer并创建一个新的可动画属性?!

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

有任何想法吗?

您可以将UIInterpolatingMotionEffect子类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类引用 。