MMPopupView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // MMPopupView.m
  3. // MMPopupView
  4. //
  5. // Created by Ralph Li on 9/6/15.
  6. // Copyright © 2015 LJC. All rights reserved.
  7. //
  8. #import "MMPopupView.h"
  9. #import "MMPopupWindow.h"
  10. #import "MMPopupDefine.h"
  11. #import "MMPopupCategory.h"
  12. #import <Masonry/Masonry.h>
  13. static NSString * const MMPopupViewHideAllNotification = @"MMPopupViewHideAllNotification";
  14. @implementation MMPopupView
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if ( self )
  19. {
  20. [self setup];
  21. }
  22. return self;
  23. }
  24. - (void)setup
  25. {
  26. self.type = MMPopupTypeAlert;
  27. self.animationDuration = 0.3f;
  28. self.attachedView = [MMPopupWindow sharedWindow].attachView;
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyHideAll:) name:MMPopupViewHideAllNotification object:nil];
  30. }
  31. - (void)dealloc
  32. {
  33. [[NSNotificationCenter defaultCenter] removeObserver:self name:MMPopupViewHideAllNotification object:nil];
  34. }
  35. - (void)notifyHideAll:(NSNotification*)n
  36. {
  37. if ( [self isKindOfClass:n.object] )
  38. {
  39. [self hide];
  40. }
  41. }
  42. + (void)hideAll
  43. {
  44. [[NSNotificationCenter defaultCenter] postNotificationName:MMPopupViewHideAllNotification object:[self class]];
  45. }
  46. - (BOOL)visible
  47. {
  48. if ( self.attachedView )
  49. {
  50. return !self.attachedView.mm_dimBackgroundView.hidden;
  51. }
  52. return NO;
  53. }
  54. - (void)setType:(MMPopupType)type
  55. {
  56. _type = type;
  57. switch (type)
  58. {
  59. case MMPopupTypeAlert:
  60. {
  61. self.showAnimation = [self alertShowAnimation];
  62. self.hideAnimation = [self alertHideAnimation];
  63. break;
  64. }
  65. case MMPopupTypeSheet:
  66. {
  67. self.showAnimation = [self sheetShowAnimation];
  68. self.hideAnimation = [self sheetHideAnimation];
  69. break;
  70. }
  71. case MMPopupTypeCustom:
  72. {
  73. self.showAnimation = [self customShowAnimation];
  74. self.hideAnimation = [self customHideAnimation];
  75. break;
  76. }
  77. default:
  78. break;
  79. }
  80. }
  81. - (void)setAnimationDuration:(NSTimeInterval)animationDuration
  82. {
  83. _animationDuration = animationDuration;
  84. self.attachedView.mm_dimAnimationDuration = animationDuration;
  85. }
  86. - (void)show
  87. {
  88. [self showWithBlock:nil];
  89. }
  90. - (void)showWithBlock:(MMPopupCompletionBlock)block
  91. {
  92. if ( block )
  93. {
  94. self.showCompletionBlock = block;
  95. }
  96. if ( !self.attachedView )
  97. {
  98. self.attachedView = [MMPopupWindow sharedWindow].attachView;
  99. }
  100. [self.attachedView mm_showDimBackground];
  101. MMPopupBlock showAnimation = self.showAnimation;
  102. NSAssert(showAnimation, @"show animation must be there");
  103. showAnimation(self);
  104. if ( self.withKeyboard )
  105. {
  106. [self showKeyboard];
  107. }
  108. }
  109. - (void)hide
  110. {
  111. [self hideWithBlock:nil];
  112. }
  113. - (void)hideWithBlock:(MMPopupCompletionBlock)block
  114. {
  115. if ( block )
  116. {
  117. self.hideCompletionBlock = block;
  118. }
  119. if ( !self.attachedView )
  120. {
  121. self.attachedView = [MMPopupWindow sharedWindow].attachView;
  122. }
  123. [self.attachedView mm_hideDimBackground];
  124. if ( self.withKeyboard )
  125. {
  126. [self hideKeyboard];
  127. }
  128. MMPopupBlock hideAnimation = self.hideAnimation;
  129. NSAssert(hideAnimation, @"hide animation must be there");
  130. hideAnimation(self);
  131. }
  132. - (MMPopupBlock)alertShowAnimation
  133. {
  134. MMWeakify(self);
  135. MMPopupBlock block = ^(MMPopupView *popupView){
  136. MMStrongify(self);
  137. if ( !self.superview )
  138. {
  139. [self.attachedView.mm_dimBackgroundView addSubview:self];
  140. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  141. make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.withKeyboard?-216/2:0));
  142. }];
  143. [self layoutIfNeeded];
  144. }
  145. self.layer.transform = CATransform3DMakeScale(1.2f, 1.2f, 1.0f);
  146. self.alpha = 0.0f;
  147. [UIView animateWithDuration:self.animationDuration
  148. delay:0.0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
  149. animations:^{
  150. self.layer.transform = CATransform3DIdentity;
  151. self.alpha = 1.0f;
  152. } completion:^(BOOL finished) {
  153. if ( self.showCompletionBlock )
  154. {
  155. self.showCompletionBlock(self, finished);
  156. }
  157. }];
  158. };
  159. return block;
  160. }
  161. - (MMPopupBlock)alertHideAnimation
  162. {
  163. MMWeakify(self);
  164. MMPopupBlock block = ^(MMPopupView *popupView){
  165. MMStrongify(self);
  166. [UIView animateWithDuration:self.animationDuration
  167. delay:0
  168. options: UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
  169. animations:^{
  170. self.alpha = 0.0f;
  171. }
  172. completion:^(BOOL finished) {
  173. if ( finished )
  174. {
  175. [self removeFromSuperview];
  176. }
  177. if ( self.hideCompletionBlock )
  178. {
  179. self.hideCompletionBlock(self, finished);
  180. }
  181. }];
  182. };
  183. return block;
  184. }
  185. - (MMPopupBlock)sheetShowAnimation
  186. {
  187. MMWeakify(self);
  188. MMPopupBlock block = ^(MMPopupView *popupView){
  189. MMStrongify(self);
  190. if ( !self.superview )
  191. {
  192. [self.attachedView.mm_dimBackgroundView addSubview:self];
  193. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  194. make.centerX.equalTo(self.attachedView);
  195. make.bottom.equalTo(self.attachedView.mas_bottom).offset(self.attachedView.frame.size.height);
  196. }];
  197. [self layoutIfNeeded];
  198. }
  199. [UIView animateWithDuration:self.animationDuration
  200. delay:0
  201. options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
  202. animations:^{
  203. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  204. make.bottom.equalTo(self.attachedView.mas_bottom).offset(0);
  205. }];
  206. [self.superview layoutIfNeeded];
  207. }
  208. completion:^(BOOL finished) {
  209. if ( self.showCompletionBlock )
  210. {
  211. self.showCompletionBlock(self, finished);
  212. }
  213. }];
  214. };
  215. return block;
  216. }
  217. - (MMPopupBlock)sheetHideAnimation
  218. {
  219. MMWeakify(self);
  220. MMPopupBlock block = ^(MMPopupView *popupView){
  221. MMStrongify(self);
  222. [UIView animateWithDuration:self.animationDuration
  223. delay:0
  224. options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
  225. animations:^{
  226. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  227. make.bottom.equalTo(self.attachedView.mas_bottom).offset(self.attachedView.frame.size.height);
  228. }];
  229. [self.superview layoutIfNeeded];
  230. }
  231. completion:^(BOOL finished) {
  232. if ( finished )
  233. {
  234. [self removeFromSuperview];
  235. }
  236. if ( self.hideCompletionBlock )
  237. {
  238. self.hideCompletionBlock(self, finished);
  239. }
  240. }];
  241. };
  242. return block;
  243. }
  244. - (MMPopupBlock)customShowAnimation
  245. {
  246. MMWeakify(self);
  247. MMPopupBlock block = ^(MMPopupView *popupView){
  248. MMStrongify(self);
  249. if ( !self.superview )
  250. {
  251. [self.attachedView.mm_dimBackgroundView addSubview:self];
  252. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  253. make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, -self.attachedView.bounds.size.height));
  254. }];
  255. [self layoutIfNeeded];
  256. }
  257. [UIView animateWithDuration:self.animationDuration
  258. delay:0
  259. usingSpringWithDamping:0.8
  260. initialSpringVelocity:1.5
  261. options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
  262. animations:^{
  263. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  264. make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.withKeyboard?-216/2:0));
  265. }];
  266. [self.superview layoutIfNeeded];
  267. } completion:^(BOOL finished) {
  268. if ( self.showCompletionBlock )
  269. {
  270. self.showCompletionBlock(self, finished);
  271. }
  272. }];
  273. };
  274. return block;
  275. }
  276. - (MMPopupBlock)customHideAnimation
  277. {
  278. MMWeakify(self);
  279. MMPopupBlock block = ^(MMPopupView *popupView){
  280. MMStrongify(self);
  281. [UIView animateWithDuration:0.3
  282. delay:0
  283. options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
  284. animations:^{
  285. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  286. make.center.equalTo(self.attachedView).centerOffset(CGPointMake(0, self.attachedView.bounds.size.height));
  287. }];
  288. [self.superview layoutIfNeeded];
  289. } completion:^(BOOL finished) {
  290. if ( finished )
  291. {
  292. [self removeFromSuperview];
  293. }
  294. if ( self.hideCompletionBlock )
  295. {
  296. self.hideCompletionBlock(self, finished);
  297. }
  298. }];
  299. };
  300. return block;
  301. }
  302. - (void)showKeyboard
  303. {
  304. }
  305. - (void)hideKeyboard
  306. {
  307. }
  308. @end