酷店

XLPageViewControllerUtil.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // XLPageViewControllerUtil.m
  3. // XLPageViewControllerExample
  4. //
  5. // Created by MengXianLiang on 2019/5/8.
  6. // Copyright © 2019 jwzt. All rights reserved.
  7. // https://github.com/mengxianliang/XLPageViewController
  8. #import "XLPageViewControllerUtil.h"
  9. @implementation XLPageViewControllerUtil
  10. + (CGFloat)widthForText:(NSString *)text font:(UIFont *)font size:(CGSize)size {
  11. NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin |
  12. NSStringDrawingUsesFontLeading;
  13. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  14. [style setLineBreakMode:NSLineBreakByTruncatingTail];
  15. NSDictionary *attributes = @{
  16. NSFontAttributeName : font,
  17. NSParagraphStyleAttributeName : style
  18. };
  19. CGSize textSize = [text boundingRectWithSize:size
  20. options:opts
  21. attributes:attributes
  22. context:nil].size;
  23. return textSize.width;
  24. }
  25. + (UIColor *)colorTransformFrom:(UIColor*)fromColor to:(UIColor *)toColor progress:(CGFloat)progress {
  26. if (!fromColor || !toColor) {
  27. NSLog(@"Warning !!! color is nil");
  28. return [UIColor blackColor];
  29. }
  30. progress = progress >= 1 ? 1 : progress;
  31. progress = progress <= 0 ? 0 : progress;
  32. const CGFloat * fromeComponents = CGColorGetComponents(fromColor.CGColor);
  33. const CGFloat * toComponents = CGColorGetComponents(toColor.CGColor);
  34. size_t fromColorNumber = CGColorGetNumberOfComponents(fromColor.CGColor);
  35. size_t toColorNumber = CGColorGetNumberOfComponents(toColor.CGColor);
  36. if (fromColorNumber == 2) {
  37. CGFloat white = fromeComponents[0];
  38. fromColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
  39. fromeComponents = CGColorGetComponents(fromColor.CGColor);
  40. }
  41. if (toColorNumber == 2) {
  42. CGFloat white = toComponents[0];
  43. toColor = [UIColor colorWithRed:white green:white blue:white alpha:1];
  44. toComponents = CGColorGetComponents(toColor.CGColor);
  45. }
  46. CGFloat red = fromeComponents[0]*(1 - progress) + toComponents[0]*progress;
  47. CGFloat green = fromeComponents[1]*(1 - progress) + toComponents[1]*progress;
  48. CGFloat blue = fromeComponents[2]*(1 - progress) + toComponents[2]*progress;
  49. return [UIColor colorWithRed:red green:green blue:blue alpha:1];
  50. }
  51. + (void)showAnimationToShadow:(UIView *)shadow shadowWidth:(CGFloat)shadowWidth fromItemRect:(CGRect)fromItemRect toItemRect:(CGRect)toItemRect type:(XLPageShadowLineAnimationType)type progress:(CGFloat)progress {
  52. //没有动画,跳过
  53. if (type == XLPageShadowLineAnimationTypeNone) {
  54. return;
  55. }
  56. //平移动画
  57. if (type == XLPageShadowLineAnimationTypePan) {
  58. CGFloat distance = CGRectGetMidX(toItemRect) - CGRectGetMidX(fromItemRect);
  59. CGFloat centerX = CGRectGetMidX(fromItemRect) + fabs(progress)*distance;
  60. shadow.center = CGPointMake(centerX, shadow.center.y);
  61. }
  62. //缩放动画
  63. if (type == XLPageShadowLineAnimationTypeZoom) {
  64. CGFloat distance = fabs(CGRectGetMidX(toItemRect) - CGRectGetMidX(fromItemRect));
  65. CGFloat fromX = CGRectGetMidX(fromItemRect) - shadowWidth/2.0f;
  66. CGFloat toX = CGRectGetMidX(toItemRect) - shadowWidth/2.0f;
  67. if (progress > 0) {//向右移动
  68. //前半段0~0.5,x不变 w变大
  69. if (progress <= 0.5) {
  70. //让过程变成0~1
  71. CGFloat newProgress = 2*fabs(progress);
  72. CGFloat newWidth = shadowWidth + newProgress*distance;
  73. CGRect shadowFrame = shadow.frame;
  74. shadowFrame.size.width = newWidth;
  75. shadowFrame.origin.x = fromX;
  76. shadow.frame = shadowFrame;
  77. }else if (progress >= 0.5) { //后半段0.5~1,x变大 w变小
  78. //让过程变成1~0
  79. CGFloat newProgress = 2*(1-fabs(progress));
  80. CGFloat newWidth = shadowWidth + newProgress*distance;
  81. CGFloat newX = toX - newProgress*distance;
  82. CGRect shadowFrame = shadow.frame;
  83. shadowFrame.size.width = newWidth;
  84. shadowFrame.origin.x = newX;
  85. shadow.frame = shadowFrame;
  86. }
  87. }else {//向左移动
  88. //前半段0~-0.5,x变小 w变大
  89. if (progress >= -0.5) {
  90. //让过程变成0~1
  91. CGFloat newProgress = 2*fabs(progress);
  92. CGFloat newWidth = shadowWidth + newProgress*distance;
  93. CGFloat newX = fromX - newProgress*distance;
  94. CGRect shadowFrame = shadow.frame;
  95. shadowFrame.size.width = newWidth;
  96. shadowFrame.origin.x = newX;
  97. shadow.frame = shadowFrame;
  98. }else if (progress <= -0.5) { //后半段-0.5~-1,x变大 w变小
  99. //让过程变成1~0
  100. CGFloat newProgress = 2*(1-fabs(progress));
  101. CGFloat newWidth = shadowWidth + newProgress*distance;
  102. CGRect shadowFrame = shadow.frame;
  103. shadowFrame.size.width = newWidth;
  104. shadowFrame.origin.x = toX;
  105. shadow.frame = shadowFrame;
  106. }
  107. }
  108. }
  109. }
  110. @end
  111. #import <objc/runtime.h>
  112. @implementation UIView (LetMeScroll)
  113. static NSString *LetMeScrollFirstKey = @"LetMeScrollFirstKey";
  114. - (void)setXl_letMeScrollFirst:(BOOL)xl_letMeScrollFirst {
  115. objc_setAssociatedObject(self, &LetMeScrollFirstKey,
  116. @(xl_letMeScrollFirst), OBJC_ASSOCIATION_ASSIGN);
  117. }
  118. - (BOOL)xl_letMeScrollFirst {
  119. return [objc_getAssociatedObject(self, &LetMeScrollFirstKey) boolValue];;
  120. }
  121. @end