FDPopView.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // FDPopView.m
  3. // FDUIKitObjC
  4. //
  5. // Created by fandongtongxue on 2020/2/26.
  6. //
  7. #import "FDPopView.h"
  8. #import "FDUIColorDefine.h"
  9. #import "FDUIDefine.h"
  10. #import "UIView+FD.h"
  11. @interface FDPopView ()
  12. @property(nonatomic, strong) UIView *shadowView;
  13. @property(nonatomic, assign) FDPopType type;
  14. @end
  15. @implementation FDPopView
  16. - (instancetype)initWithFrame:(CGRect)frame{
  17. if (self = [super initWithFrame:frame]) {
  18. self.backgroundColor = FD_WhiteColor;
  19. self.fd_y = FD_ScreenHeight;
  20. }
  21. return self;
  22. }
  23. - (void)show:(UIView *)superView type:(FDPopType)type{
  24. if (type == FDPopTypeTop) {
  25. self.fd_y = - self.fd_height;
  26. }
  27. _type = type;
  28. [superView addSubview:self.shadowView];
  29. [superView addSubview:self];
  30. __weak __typeof(self)weakSelf = self;
  31. [UIView animateWithDuration:0.25 animations:^{
  32. __strong __typeof(weakSelf)strongSelf = weakSelf;
  33. CGFloat offsetY = 0;
  34. switch (type) {
  35. case FDPopTypeBottom:
  36. offsetY = superView.fd_height - strongSelf.fd_height;
  37. break;
  38. case FDPopTypeCenter:
  39. offsetY = ( superView.fd_height - strongSelf.fd_height ) / 2;
  40. break;
  41. case FDPopTypeTop:
  42. offsetY = FD_StatusBar_Height;
  43. break;
  44. default:
  45. break;
  46. }
  47. strongSelf.frame = CGRectMake(strongSelf.fd_left, offsetY, strongSelf.fd_width, strongSelf.fd_height);
  48. }];
  49. }
  50. - (void)hide{
  51. __weak __typeof(self)weakSelf = self;
  52. CGFloat offsetY = self.superview.fd_height;
  53. if (_type == FDPopTypeTop) {
  54. offsetY = - self.fd_height;
  55. }
  56. [UIView animateWithDuration:0.25 animations:^{
  57. __strong __typeof(weakSelf)strongSelf = weakSelf;
  58. strongSelf.frame = CGRectMake(strongSelf.fd_left, offsetY, strongSelf.fd_width, strongSelf.fd_height);
  59. } completion:^(BOOL finished) {
  60. __strong __typeof(weakSelf)strongSelf = weakSelf;
  61. [strongSelf.shadowView removeFromSuperview];
  62. [strongSelf removeFromSuperview];
  63. }];
  64. }
  65. - (UIView *)shadowView{
  66. if (!_shadowView) {
  67. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, FD_ScreenWidth, FD_ScreenHeight)];
  68. _shadowView.backgroundColor = [FD_BlackColor colorWithAlphaComponent:0.4];
  69. _shadowView.userInteractionEnabled = YES;
  70. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  71. [_shadowView addGestureRecognizer:tap];
  72. }
  73. return _shadowView;
  74. }
  75. @end