悟空记账

HCBasePopupViewController.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // HCCenterPopupViewController.m
  3. // Test2
  4. //
  5. // Created by hehaichi on 2017/12/15.
  6. // Copyright © 2017年 hehaichi. All rights reserved.
  7. //
  8. #import "HCBasePopupViewController.h"
  9. #define kHCBasePopupViewDefaultSize CGSizeMake(CGRectGetWidth(self.view.frame) * 0.75, CGRectGetHeight(self.view.frame) * 0.6)
  10. @interface HCBasePopupViewController ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>{
  11. HCBasePopupAnimatingType animatingType;
  12. }
  13. @property(nonatomic,strong)UIView * maskView;
  14. @property (nonatomic,strong) UITapGestureRecognizer *tapGesture;
  15. @end
  16. @implementation HCBasePopupViewController
  17. - (instancetype)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. _tapMaskDissmiss = YES;
  22. _popupViewCornerRadius = 4;
  23. self.transitioningDelegate = self;
  24. self.modalPresentationStyle = UIModalPresentationCustom;
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self.view addSubview:self.maskView];
  31. [self.view addSubview:self.popupView];
  32. [self.maskView addGestureRecognizer:self.tapGesture];
  33. if ([self.popupDelegate respondsToSelector:@selector(hcPopViewController:setupSubViewWithPopupView:)]) {
  34. [self.popupDelegate hcPopViewController:self setupSubViewWithPopupView:self.popupView];
  35. }
  36. }
  37. - (void)didReceiveMemoryWarning {
  38. [super didReceiveMemoryWarning];
  39. }
  40. #pragma mark - setter
  41. - (void)setPopupViewSize:(CGSize)popupViewSize{
  42. _popupViewSize = popupViewSize;
  43. CGPoint center = self.popupView.center;
  44. CGRect frame = self.popupView.frame;
  45. frame.size = popupViewSize;
  46. self.popupView.frame = frame;
  47. self.popupView.center = center;
  48. }
  49. - (void)setMaskColor:(UIColor *)maskColor{
  50. _maskColor = maskColor;
  51. self.maskView.backgroundColor = maskColor;
  52. }
  53. - (void)setPopupViewInsets:(UIEdgeInsets)popupViewInsets{
  54. _popupViewInsets = popupViewInsets;
  55. CGPoint center = self.popupView.center;
  56. center = CGPointMake(center.x+popupViewInsets.left-popupViewInsets.right, center.y+popupViewInsets.top-popupViewInsets.bottom);
  57. self.popupView.center = center;
  58. }
  59. #pragma mark - getter
  60. - (UIView *)popupView {
  61. if (!_popupView){
  62. CGSize size;
  63. if (self.popupViewSize.width > 0 && self.popupViewSize.height > 0) {
  64. size = self.popupViewSize;
  65. }else{
  66. size = kHCBasePopupViewDefaultSize;
  67. }
  68. _popupView = [[UIView alloc]initWithFrame:CGRectMake(self.view.center.x - size.width/2+self.popupViewInsets.left - self.popupViewInsets.right, self.view.center.y -size.height/2+self.popupViewInsets.top - self.popupViewInsets.bottom, size.width, size.height)];
  69. _popupView.backgroundColor = [UIColor whiteColor];
  70. _popupView.layer.cornerRadius = self.popupViewCornerRadius;
  71. _popupView.layer.masksToBounds = YES;
  72. _popupView.tag = 1000;
  73. }
  74. return _popupView;
  75. }
  76. - (UIView *)maskView {
  77. if (!_maskView) {
  78. _maskView = [[UIView alloc]initWithFrame:self.view.frame];
  79. _maskView.backgroundColor = self.maskColor?self.maskColor:[[UIColor blackColor]colorWithAlphaComponent:0.5];
  80. [_maskView addGestureRecognizer:self.tapGesture];
  81. }
  82. return _maskView;
  83. }
  84. - (UITapGestureRecognizer *)tapGesture {
  85. if (!_tapGesture) {
  86. _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
  87. }
  88. return _tapGesture;
  89. }
  90. #pragma mark - target selector
  91. - (void)closeAction {
  92. [self dismiss];
  93. }
  94. - (void)tapAction:(UITapGestureRecognizer *)tapGesture {
  95. if ([self.popupDelegate respondsToSelector:@selector(hcPopViewController:didTapMaskViewWithMaskView:)]) {
  96. [self.popupDelegate hcPopViewController:self didTapMaskViewWithMaskView:self.maskView];
  97. }
  98. if (self.tapMaskDissmiss) {
  99. [self dismiss];
  100. }
  101. }
  102. #pragma mark - dismiss
  103. - (void)dismiss {
  104. [self dismissViewControllerAnimated:YES completion:nil];
  105. }
  106. - (void)dismissdWithCompletion:(void (^ __nullable)(void))completion {
  107. [self dismissViewControllerAnimated:YES completion:completion];
  108. }
  109. #pragma mark - UIViewControllerAnimatedTransitioning
  110. - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
  111. return self.animatedTimeInterval>0?self.animatedTimeInterval:0.4;
  112. }
  113. - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
  114. UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  115. UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  116. UIView *containerView = [transitionContext containerView];
  117. if (animatingType == HCBasePopupAnimatingTypePresent) {
  118. UIView *popedView = [toVC.view viewWithTag:1000];
  119. [containerView addSubview:toVC.view];
  120. popedView.alpha = 0;
  121. popedView.transform = CGAffineTransformMakeScale(0.3, 0.3);
  122. NSTimeInterval duration = 0.5;
  123. [UIView animateWithDuration:duration / 2.0 animations:^{
  124. popedView.alpha = 1.0;
  125. }];
  126. CGFloat damping = 0.55;
  127. //回弹动画
  128. [UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:damping initialSpringVelocity:1.0 / damping options:0 animations:^{
  129. popedView.transform = CGAffineTransformIdentity;
  130. } completion:^(BOOL finished) {
  131. [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
  132. }];
  133. } else {
  134. UIView *popedView = [fromVC.view viewWithTag:1000];
  135. NSTimeInterval duration = 0.25;
  136. [UIView animateWithDuration:duration animations:^{
  137. popedView.alpha = 0;
  138. } completion:^(BOOL finished) {
  139. [transitionContext completeTransition:YES];
  140. }];
  141. }
  142. }
  143. #pragma mark - UIViewControllerTransitioningDelegate
  144. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  145. animatingType = HCBasePopupAnimatingTypePresent;
  146. return self;
  147. }
  148. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  149. animatingType = HCBasePopupAnimatingTypeDismiss;
  150. return self;
  151. }
  152. - (void)dealloc{
  153. NSLog(@"%s",__FUNCTION__);
  154. }
  155. @end