HWPanIndicatorView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // HWPanIndicatorView.m
  3. // HWPanModal
  4. //
  5. // Created by heath wang on 2019/5/16.
  6. //
  7. #import "UIView+HW_Frame.h"
  8. #import <HWPanModal/HWPanIndicatorView.h>
  9. @interface HWPanIndicatorView ()
  10. @property (nonatomic, strong) UIView *leftView;
  11. @property (nonatomic, strong) UIView *rightView;
  12. @property (nonatomic, assign) HWIndicatorState state;
  13. @end
  14. @implementation HWPanIndicatorView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. self = [super initWithFrame:CGRectZero];
  17. if (self) {
  18. self.backgroundColor = [UIColor clearColor];
  19. [self addSubview:self.leftView];
  20. [self addSubview:self.rightView];
  21. self.indicatorColor = [UIColor colorWithRed:0.792 green:0.788 blue:0.812 alpha:1.00];
  22. }
  23. return self;
  24. }
  25. - (void)animate:(void (^)(void))animations {
  26. [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut animations:animations completion:^(BOOL finished) {
  27. }];
  28. }
  29. #pragma mark - HWPanModalIndicatorProtocol
  30. - (void)didChangeToState:(HWIndicatorState)state {
  31. self.state = state;
  32. }
  33. - (CGSize)indicatorSize {
  34. return CGSizeMake(34, 13);
  35. }
  36. - (void)setupSubviews {
  37. CGSize size = [self indicatorSize];
  38. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height);
  39. CGFloat height = 5;
  40. CGFloat correction = height / 2;
  41. self.leftView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame) / 2 + correction, height);
  42. self.leftView.hw_centerY = self.hw_height / 2;
  43. self.leftView.layer.cornerRadius = MIN(self.leftView.hw_width, self.leftView.hw_height) / 2;
  44. self.rightView.frame = CGRectMake(CGRectGetWidth(self.frame) / 2 - correction, 0, CGRectGetWidth(self.frame) / 2 + correction, height);
  45. self.rightView.hw_centerY = self.hw_height / 2;
  46. self.rightView.layer.cornerRadius = MIN(self.rightView.hw_width, self.rightView.hw_height) / 2;
  47. }
  48. #pragma mark - Getter
  49. - (UIView *)leftView {
  50. if (!_leftView) {
  51. _leftView = [UIView new];
  52. }
  53. return _leftView;
  54. }
  55. - (UIView *)rightView {
  56. if (!_rightView) {
  57. _rightView = [UIView new];
  58. }
  59. return _rightView;
  60. }
  61. #pragma mark - Setter
  62. - (void)setIndicatorColor:(UIColor *)indicatorColor {
  63. _indicatorColor = indicatorColor;
  64. self.leftView.backgroundColor = indicatorColor;
  65. self.rightView.backgroundColor = indicatorColor;
  66. }
  67. - (void)setState:(HWIndicatorState)state {
  68. _state = state;
  69. switch (state) {
  70. case HWIndicatorStateNormal: {
  71. CGFloat angle = 20 * M_PI / 180;
  72. [self animate:^{
  73. self.leftView.transform = CGAffineTransformMakeRotation(angle);
  74. self.rightView.transform = CGAffineTransformMakeRotation(-angle);
  75. }];
  76. }
  77. break;
  78. case HWIndicatorStatePullDown: {
  79. [self animate:^{
  80. self.leftView.transform = CGAffineTransformIdentity;
  81. self.rightView.transform = CGAffineTransformIdentity;
  82. }];
  83. }
  84. break;
  85. }
  86. }
  87. @end