只需显示UIInterpolatingMotionEffect的值?

Fat*_*tie 2 key-value-observing calayer uiview ios uimotion

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

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

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

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

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

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

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

有任何想法吗?

Cha*_*der 7

您可以子类化UIInterpolatingMotionEffect并挂钩到方法中-(NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset以记录viewerOffset获取规范化值或查看超级实现返回的字典以获取最小值和最大值之间的插值.

UILogInterpolatingMotionEffect.h

@interface UILogInterpolatingMotionEffect : UIInterpolatingMotionEffect

@end
Run Code Online (Sandbox Code Playgroud)

UILogInterpolatingMotionEffect.m

@implementation UILogInterpolatingMotionEffect

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

@end
Run Code Online (Sandbox Code Playgroud)

PS:如果你只对viewerOffset感兴趣,你甚至可以继承UIMotionEffect.另请参见UIMotionEffect类引用.