悟空记账

HCBottomPopupViewController.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // HCBottomPopupViewController.m
  3. // Test2
  4. //
  5. // Created by hehaichi on 2017/12/15.
  6. // Copyright © 2017年 hehaichi. All rights reserved.
  7. //
  8. #import "HCBottomPopupViewController.h"
  9. #import "HCPopupCommon.h"
  10. #define HCBottomPopupSelectItemHeight 50
  11. @interface HCBottomPopViewSelectedItemView:UIButton
  12. @property(nonatomic,strong)UIView * bottomLine;
  13. @property(nonatomic,strong)NSString * title;
  14. @property(nonatomic,copy)dispatch_block_t clickBlock;
  15. @property(nonatomic,assign)HCBottomPopupActionSelectItemType type;
  16. @end
  17. @implementation HCBottomPopViewSelectedItemView
  18. - (instancetype)initWithFrame:(CGRect)frame withType:(HCBottomPopupActionSelectItemType)type{
  19. if(self = [super initWithFrame:frame]){
  20. _type = type;
  21. [self addSubview:self.bottomLine];
  22. self.titleLabel.font = [UIFont systemFontOfSize:14];
  23. self.backgroundColor = [UIColor whiteColor];
  24. [self addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  25. }
  26. return self;
  27. }
  28. - (void)setTitle:(NSString *)title{
  29. _title = title;
  30. [self setTitle:_title forState:UIControlStateNormal];
  31. [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  32. }
  33. - (UIView *)bottomLine{
  34. if (!_bottomLine) {
  35. _bottomLine = [[UIView alloc]init];
  36. _bottomLine.frame = CGRectMake(0, CGRectGetHeight(self.frame)-1, CGRectGetWidth(self.frame), 1);
  37. _bottomLine.backgroundColor = [UIColor blackColor];
  38. _bottomLine.alpha = 0.1;
  39. }
  40. return _bottomLine;
  41. }
  42. - (void)action:(UIButton *)sender{
  43. if (self.clickBlock) {
  44. self.clickBlock();
  45. }
  46. }
  47. @end
  48. @interface HCBottomPopupViewController ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning,HCBasePopupViewControllerDelegate>
  49. {
  50. HCBasePopupAnimatingType animatingType;
  51. }
  52. @property(nonatomic,strong)NSMutableArray <HCBottomPopupAction *>* actionArray;
  53. @property(nonatomic,assign)BOOL isContainCancel;
  54. @end
  55. @implementation HCBottomPopupViewController
  56. - (instancetype)init
  57. {
  58. self = [super init];
  59. if (self) {
  60. _actionArray = @[].mutableCopy;
  61. self.popupViewCornerRadius = 0;
  62. self.tapMaskDissmiss = YES;
  63. self.popupDelegate = self;
  64. }
  65. return self;
  66. }
  67. - (void)viewDidLoad {
  68. [self setupPopViewSize];
  69. [super viewDidLoad];
  70. }
  71. #pragma mark - private method
  72. - (void)setupPopViewSize{
  73. CGFloat height = self.actionArray.count * HCBottomPopupSelectItemHeight;
  74. if (self.isContainCancel) {
  75. height += 20;
  76. }
  77. self.popupViewSize = CGSizeMake(CGRectGetWidth(self.view.frame),height);
  78. }
  79. - (void)didReceiveMemoryWarning {
  80. [super didReceiveMemoryWarning];
  81. // Dispose of any resources that can be recreated.
  82. }
  83. #pragma mark - public method
  84. - (void)addAction:(HCBottomPopupAction *)action{
  85. if (![self.actionArray containsObject:action]) {
  86. if (self.isContainCancel == YES) {
  87. return;
  88. }
  89. if (action.type == HCBottomPopupActionSelectItemTypeCancel && self.isContainCancel == NO) {
  90. self.isContainCancel = YES;
  91. }
  92. [self.actionArray addObject:action];
  93. }
  94. }
  95. #pragma mark - UIViewControllerAnimatedTransitioning
  96. - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
  97. return self.animatedTimeInterval>0?self.animatedTimeInterval:0.4;
  98. }
  99. - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
  100. UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  101. UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  102. UIView *containerView = [transitionContext containerView];
  103. if (animatingType == HCBasePopupAnimatingTypePresent) {
  104. UIView *popedView = [toVC.view viewWithTag:1000];
  105. [containerView addSubview:toVC.view];
  106. popedView.alpha = 0;
  107. CGRect frame = popedView.frame;
  108. frame.origin = CGPointMake(0, CGRectGetHeight(self.view.frame));
  109. popedView.frame = frame;
  110. NSTimeInterval duration = 0.4;
  111. [UIView animateWithDuration:duration / 2.0 animations:^{
  112. popedView.alpha = 1.0;
  113. }];
  114. [UIView animateWithDuration:duration / 2.0 animations:^{
  115. popedView.transform = CGAffineTransformMakeTranslation(0, -CGRectGetHeight(popedView.frame));
  116. } completion:^(BOOL finished) {
  117. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  118. }];
  119. } else {
  120. UIView *popedView = [fromVC.view viewWithTag:1000];
  121. NSTimeInterval duration = 0.25;
  122. UIButton *closeBtn = [fromVC.view viewWithTag:1001];
  123. [UIView animateWithDuration:duration animations:^{
  124. popedView.transform = CGAffineTransformMakeTranslation(0,CGRectGetHeight(popedView.frame));
  125. popedView.alpha = 0;
  126. closeBtn.alpha = 0;
  127. // popedView.transform = CGAffineTransformMakeScale(0.7, 0.7);
  128. } completion:^(BOOL finished) {
  129. [transitionContext completeTransition:YES];
  130. }];
  131. }
  132. }
  133. #pragma mark - UIViewControllerTransitioningDelegate
  134. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  135. animatingType = HCBasePopupAnimatingTypePresent;
  136. return self;
  137. }
  138. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  139. animatingType = HCBasePopupAnimatingTypeDismiss;
  140. return self;
  141. }
  142. #pragma mark - HCBasePopupViewControllerDelegate
  143. - (void)hcPopViewController:(UIViewController *)controller setupSubViewWithPopupView:(UIView *)popupView{
  144. popupView.backgroundColor = CWIPColorFromHex(0xcccccc);
  145. popupView.alpha = 0.4;
  146. for (HCBottomPopupAction * action in self.actionArray) {
  147. if (action.type == HCBottomPopupActionSelectItemTypeCancel) {//如果等于取消;
  148. //将action 移动到数组末尾
  149. [self.actionArray removeObject:action];
  150. [self.actionArray addObject:action];
  151. }
  152. }
  153. for (int i = 0; i < self.actionArray.count; i++) {
  154. spweakify(self);
  155. HCBottomPopupAction * action = self.actionArray[i];
  156. HCBottomPopViewSelectedItemView * sv;
  157. if (action.type == HCBottomPopupActionSelectItemTypeCancel) {
  158. sv = [[HCBottomPopViewSelectedItemView alloc]initWithFrame:CGRectMake(0, i*HCBottomPopupSelectItemHeight+20, CGRectGetWidth(popupView.frame), HCBottomPopupSelectItemHeight) withType:action.type];
  159. sv.clickBlock = ^{
  160. spstrongify(self);
  161. if (action.selectBlock) {
  162. action.selectBlock();
  163. }
  164. [self dismiss];
  165. };
  166. }else{
  167. sv = [[HCBottomPopViewSelectedItemView alloc]initWithFrame:CGRectMake(0, i*HCBottomPopupSelectItemHeight, CGRectGetWidth(popupView.frame), HCBottomPopupSelectItemHeight) withType:action.type];
  168. sv.clickBlock = ^{
  169. if (action.selectBlock) {
  170. action.selectBlock();
  171. }
  172. };
  173. }
  174. [popupView addSubview:sv];
  175. sv.title = action.title;
  176. }
  177. }
  178. @end