react-native-navigation的迁移库

TextStorageTransition.m 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #import "TextStorageTransition.h"
  2. #import "RNNInterpolator.h"
  3. @implementation TextStorageTransition {
  4. UIColor* _fromColor;
  5. UIColor* _toColor;
  6. UIFont* _fromFont;
  7. UIFont* _toFont;
  8. }
  9. - (instancetype)initWithView:(UIView *)view from:(NSTextStorage *)from to:(NSTextStorage *)to startDelay:(NSTimeInterval)startDelay duration:(NSTimeInterval)duration interpolation:(Text *)interpolation {
  10. self = [super initWithView:view startDelay:startDelay duration:duration interpolation:interpolation];
  11. _from = from;
  12. _to = to;
  13. [self prepareAnimationValues];
  14. return self;
  15. }
  16. - (void)prepareAnimationValues {
  17. NSRange range1;
  18. NSAttributedString* fromAttributes = [_from attributedSubstringFromRange:NSMakeRange(0, _from.string.length)];
  19. NSAttributedString* toAttributes = [_to attributedSubstringFromRange:NSMakeRange(0, _to.string.length)];
  20. _fromColor = [fromAttributes attribute:NSForegroundColorAttributeName atIndex:0 longestEffectiveRange:&range1 inRange:NSMakeRange(0, _from.string.length)] ?: UIColor.blackColor;
  21. _toColor = [toAttributes attribute:NSForegroundColorAttributeName atIndex:0 longestEffectiveRange:&range1 inRange:NSMakeRange(0, _to.string.length)] ?: UIColor.blackColor;
  22. _fromFont = [fromAttributes attribute:NSFontAttributeName atIndex:0 longestEffectiveRange:&range1 inRange:NSMakeRange(0, _from.string.length)];
  23. _toFont = [toAttributes attribute:NSFontAttributeName atIndex:0 longestEffectiveRange:&range1 inRange:NSMakeRange(0, _to.string.length)];
  24. }
  25. - (CATransform3D)animateWithProgress:(CGFloat)p {
  26. NSRange range = NSMakeRange(0, _from.string.length);
  27. UIColor* color = [RNNInterpolator fromColor:_fromColor toColor:_toColor precent:p];
  28. [_from addAttribute:NSForegroundColorAttributeName value:color range:range];
  29. CGFloat pointSize = [RNNInterpolator fromFloat:_fromFont.pointSize toFloat:_toFont.pointSize precent:p interpolation:self.interpolation];
  30. [_from addAttribute:NSFontAttributeName value:[_toFont fontWithSize:pointSize] range:range];
  31. return CATransform3DIdentity;
  32. }
  33. - (void)end {
  34. NSRange range = NSMakeRange(0, _from.string.length);
  35. [_from addAttribute:NSFontAttributeName value:_fromFont range:range];
  36. [_from addAttribute:NSForegroundColorAttributeName value:_fromColor range:range];
  37. }
  38. @end