YMPopupView.m 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. //
  2. // YMPopupView.m
  3. // yuemoClient
  4. //
  5. // Created by YoMi on 2023/11/6.
  6. //
  7. #import "YMPopupView.h"
  8. #import "YMTimer.h"
  9. #import <objc/runtime.h>
  10. static NSString *const YMPopupViewLogTitle = @"YMPopupView日志 ---> :";
  11. static YMPopupViewLogStyle _logStyle;
  12. @interface YMPopupViewManager : NSObject
  13. /** 单例 */
  14. YMPopupViewManager *YMPopupViewM(void);
  15. @end
  16. @interface YMPopupViewManager ()
  17. /** 储存已经展示popView */
  18. @property (nonatomic,strong) NSMutableArray *popViewMarr;
  19. /** 按顺序弹窗顺序存储已显示的 popview */
  20. @property (nonatomic,strong) NSPointerArray *showList;
  21. /** 储存待移除的popView */
  22. @property (nonatomic,strong) NSHashTable <YMPopupView *> *removeList;
  23. /** 内存信息View */
  24. @property (nonatomic,strong) UILabel *infoView;
  25. @end
  26. @implementation YMPopupViewManager
  27. static YMPopupViewManager *_instance;
  28. YMPopupViewManager *YMPopupViewM() {
  29. return [YMPopupViewManager sharedInstance];
  30. }
  31. + (instancetype)sharedInstance {
  32. if (!_instance) {
  33. static dispatch_once_t onceToken;
  34. dispatch_once(&onceToken, ^{
  35. _instance = [[self alloc] init];
  36. });
  37. }
  38. return _instance;
  39. }
  40. + (id)allocWithZone:(struct _NSZone *)zone {
  41. static dispatch_once_t onceToken;
  42. dispatch_once(&onceToken, ^{
  43. _instance = [super allocWithZone:zone];
  44. });
  45. return _instance;
  46. }
  47. + (NSArray *)getAllPopView {
  48. return YMPopupViewM().popViewMarr;
  49. }
  50. /** 获取当前页面所有popView */
  51. + (NSArray *)getAllPopViewForParentView:(UIView *)parentView {
  52. NSMutableArray *mArr = YMPopupViewM().popViewMarr;
  53. NSMutableArray *resMarr = [NSMutableArray array];
  54. for (id obj in mArr) {
  55. YMPopupView *popView = (YMPopupView *)obj;
  56. if ([popView.parentView isEqual:parentView]) {
  57. [resMarr addObject:obj];
  58. }
  59. }
  60. return [NSArray arrayWithArray:resMarr];
  61. }
  62. /** 获取当前页面指定编队的所有popView */
  63. + (NSArray *)getAllPopViewForPopView:(YMPopupView *)popView {
  64. NSArray *mArr = [self getAllPopViewForParentView:popView.parentView];
  65. NSMutableArray *resMarr = [NSMutableArray array];
  66. for (id obj in mArr) {
  67. YMPopupView *tPopView = (YMPopupView *)obj;
  68. if (popView.groupId == nil && tPopView.groupId == nil) {
  69. [resMarr addObject:obj];
  70. continue;
  71. }
  72. if ([tPopView.groupId isEqual:popView.groupId]) {
  73. [resMarr addObject:obj];
  74. continue;
  75. }
  76. }
  77. return [NSArray arrayWithArray:resMarr];
  78. }
  79. /** 读取popView */
  80. + (YMPopupView *)getPopViewForKey:(NSString *)key {
  81. if (key.length<=0) { return nil; }
  82. NSMutableArray *mArr = YMPopupViewM().popViewMarr;
  83. for (id obj in mArr) {
  84. YMPopupView *popView = (YMPopupView *)obj;
  85. if ([popView.identifier isEqualToString:key]) {
  86. return popView;
  87. }
  88. }
  89. return nil;
  90. }
  91. + (void)savePopView:(YMPopupView *)popView {
  92. [YMPopupViewM().popViewMarr addObject:popView];
  93. //优先级排序
  94. [self sortingArr];
  95. if (_logStyle & YMPopupViewLogStyleWindow) {
  96. [self setInfoData];
  97. }
  98. if (_logStyle & YMPopupViewLogStyleConsole) {
  99. [self setConsoleLog];
  100. }
  101. }
  102. /** 移除popView */
  103. + (void)removePopView:(YMPopupView *)popView {
  104. if (!popView) { return; }
  105. NSArray *arr = YMPopupViewM().popViewMarr;
  106. for (id obj in arr) {
  107. YMPopupView *tPopView = (YMPopupView *)obj;
  108. if ([tPopView isEqual:popView]) {
  109. [tPopView dismissWithStyle:YMDismissStyleNO];
  110. break;
  111. }
  112. }
  113. if (_logStyle & YMPopupViewLogStyleWindow) {
  114. [self setInfoData];
  115. }
  116. if (_logStyle & YMPopupViewLogStyleConsole) {
  117. [self setConsoleLog];
  118. }
  119. }
  120. /** 移除popView */
  121. + (void)removePopViewForKey:(NSString *)key complete:(YmVoidBlock)complete {
  122. if (key.length<=0) {
  123. complete ? complete() : nil;
  124. return;
  125. }
  126. NSArray *arr = YMPopupViewM().popViewMarr;
  127. YMPopupView *temp;
  128. for (id obj in arr) {
  129. YMPopupView *tPopView = (YMPopupView *)obj;
  130. if ([tPopView.identifier isEqualToString:key]) {
  131. temp = tPopView;
  132. tPopView.popViewDidDismissBlock = complete;
  133. [tPopView dismissWithStyle:YMDismissStyleNO];
  134. break;
  135. }
  136. }
  137. /// 此 key 没有对应的 popview. 也应该回调
  138. if (temp == nil) {
  139. complete ? complete() : nil;
  140. }
  141. if (_logStyle & YMPopupViewLogStyleWindow) {
  142. [self setInfoData];
  143. }
  144. if (_logStyle & YMPopupViewLogStyleConsole) {
  145. [self setConsoleLog];
  146. }
  147. }
  148. /** 移除所有popView */
  149. + (void)removeAllPopViewComplete:(YmVoidBlock)complete {
  150. NSMutableArray *arr = YMPopupViewM().popViewMarr;
  151. if (arr.count <= 0) {
  152. complete ? complete() : nil;
  153. return;
  154. }
  155. NSArray *popViewMarr = [NSArray arrayWithArray:YMPopupViewM().popViewMarr];
  156. NSInteger index = popViewMarr.count;
  157. [popViewMarr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
  158. YMPopupView *tPopView = (YMPopupView *)obj;
  159. if (index == idx + 1) {
  160. if (tPopView) {
  161. tPopView.popViewDidDismissBlock = complete;
  162. }else {
  163. complete ? complete() : nil;
  164. }
  165. }
  166. [tPopView dismissWithStyle:YMDismissStyleNO];
  167. }];
  168. if (_logStyle & YMPopupViewLogStyleWindow) {
  169. [self setInfoData];
  170. }
  171. if (_logStyle & YMPopupViewLogStyleConsole) {
  172. [self setConsoleLog];
  173. }
  174. }
  175. + (void)removeLastPopViewComplete:(YmVoidBlock)complete {
  176. NSPointerArray *showList = YMPopupViewM().showList;
  177. if (showList.allObjects.count == 0) {
  178. complete ? complete() : nil;
  179. return;
  180. }
  181. for (NSInteger i = showList.count - 1; i >= 0; i --) {
  182. YMPopupView *popView = [showList pointerAtIndex:i];
  183. if (popView) {
  184. popView.popViewDidDismissBlock = complete;
  185. [self removePopView:popView];
  186. [showList addPointer:NULL];
  187. [showList compact];
  188. break;
  189. }
  190. }
  191. }
  192. #pragma mark - ***** Other 其他 *****
  193. + (void)setInfoData {
  194. YMPopupViewM().infoView.text = [NSString stringWithFormat:@"S:%zd R:%zd",YMPopupViewM().popViewMarr.count,YMPopupViewM().removeList.allObjects.count];
  195. }
  196. + (void)setConsoleLog {
  197. NSLog(@"%@ S:%zd个 R:%zd个",YMPopupViewLogTitle,YMPopupViewM().popViewMarr.count,YMPopupViewM().removeList.allObjects.count);
  198. }
  199. //冒泡排序
  200. + (void)sortingArr {
  201. NSMutableArray *arr = YMPopupViewM().popViewMarr;
  202. for (int i = 0; i < arr.count; i++) {
  203. for (int j = i+1; j < arr.count; j++) {
  204. YMPopupView *iPopView = (YMPopupView *)arr[i];
  205. YMPopupView *jPopView = (YMPopupView *)arr[j];
  206. if (iPopView.priority > jPopView.priority) {
  207. [arr exchangeObjectAtIndex:i withObjectAtIndex:j];
  208. }
  209. }
  210. }
  211. }
  212. /** 转移popView到待移除队列 */
  213. + (void)transferredToRemoveQueueWithPopView:(YMPopupView *)popView {
  214. NSArray *popViewMarr = YMPopupViewM().popViewMarr;
  215. [popViewMarr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
  216. YMPopupView *tPopView = (YMPopupView *)obj;
  217. if ([popView isEqual:tPopView]) {
  218. [YMPopupViewM().removeList addObject:obj];
  219. [YMPopupViewM().popViewMarr removeObject:obj];
  220. }
  221. }];
  222. }
  223. + (void)destroyInRemoveQueue:(YMPopupView *)popView {
  224. if (YMPopupViewM().removeList.allObjects.count <= 0) {
  225. [self transferredToRemoveQueueWithPopView:popView];
  226. }
  227. if (_logStyle&YMPopupViewLogStyleWindow) {
  228. [self setInfoData];
  229. }
  230. if (_logStyle&YMPopupViewLogStyleConsole) {
  231. [self setConsoleLog];
  232. }
  233. }
  234. #pragma mark - ***** 懒加载 *****
  235. - (UILabel *)infoView {
  236. if(_infoView) return _infoView;
  237. _infoView = [[UILabel alloc] init];
  238. _infoView.backgroundColor = UIColor.orangeColor;
  239. _infoView.textAlignment = NSTextAlignmentCenter;
  240. _infoView.font = [UIFont systemFontOfSize:11];
  241. _infoView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 70, [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0? 40:20, 60, 10);
  242. _infoView.layer.cornerRadius = 1;
  243. _infoView.layer.masksToBounds = YES;
  244. UIView *superView = [UIApplication sharedApplication].keyWindow;
  245. [superView addSubview:_infoView];
  246. return _infoView;
  247. }
  248. - (NSMutableArray *)popViewMarr {
  249. if(_popViewMarr) return _popViewMarr;
  250. _popViewMarr = [NSMutableArray array];
  251. return _popViewMarr;
  252. }
  253. - (NSHashTable<YMPopupView *> *)removeList {
  254. if (_removeList) return _removeList;
  255. _removeList = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
  256. return _removeList;
  257. }
  258. - (NSPointerArray *)showList {
  259. if (_showList) return _showList;
  260. _showList = [NSPointerArray pointerArrayWithOptions:(NSPointerFunctionsWeakMemory)];
  261. return _showList;
  262. }
  263. @end
  264. static const NSTimeInterval YMPopupViewDefaultDuration = -1.0f;
  265. // 角度转弧度
  266. #define LST_DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
  267. @interface YMPopupViewBgView : UIView
  268. /** 是否隐藏背景 默认NO */
  269. @property (nonatomic, assign) BOOL isHideBg;
  270. @end
  271. @implementation YMPopupViewBgView
  272. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  273. UIView *hitView = [super hitTest:point withEvent:event];
  274. if(hitView == self && self.isHideBg){
  275. return nil;
  276. }
  277. return hitView;
  278. }
  279. @end
  280. @interface YMPopupView () <UIGestureRecognizerDelegate>
  281. /** 弹窗容器 默认是app UIWindow 可指定view作为容器 */
  282. @property (nonatomic, weak) UIView *container;
  283. /** 背景层 */
  284. @property (nonatomic, strong) YMPopupViewBgView *backgroundView;
  285. /** 自定义视图 */
  286. @property (nonatomic,strong) UIView *customView;
  287. /** 规避键盘偏移量 */
  288. @property (nonatomic, assign) CGFloat avoidKeyboardOffset;
  289. /** 代理池 */
  290. @property (nonatomic, strong) NSHashTable <id<YMPopupViewProtocol>> *delegates;
  291. /** 背景点击手势 */
  292. @property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
  293. /** 自定义View滑动手势 */
  294. @property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
  295. //当前正在拖拽的是否是tableView
  296. @property (nonatomic, assign) BOOL isDragScrollView;
  297. /** 标记popView中是否有UIScrollView, UITableView, UICollectionView */
  298. @property (nonatomic, weak) UIScrollView *scrollerView;
  299. /** 记录自定义view原始Frame */
  300. @property (nonatomic, assign) CGRect originFrame;
  301. /** 标记dragDismissStyle是否被复制 */
  302. @property (nonatomic, assign) BOOL isDragDismissStyle;
  303. /** 是否弹出键盘 */
  304. @property (nonatomic,assign,readonly) BOOL isShowKeyboard;
  305. /** 弹出键盘的高度 */
  306. @property (nonatomic, assign) CGFloat keyboardY;
  307. /// 关闭完成回调集
  308. @property (nonatomic , strong) NSHashTable * popViewDidDismissBlocks;
  309. @end
  310. @implementation YMPopupView
  311. #pragma mark - ***** 初始化 *****
  312. - (instancetype)initWithFrame:(CGRect)frame {
  313. if (self = [super initWithFrame:frame]) {
  314. [self initSubViews];
  315. }
  316. return self;
  317. }
  318. - (void)initSubViews {
  319. //初始化配置
  320. _isClickBgDismiss = NO;
  321. _isObserverScreenRotation = YES;
  322. _bgAlpha = 0.25;
  323. _popStyle = YMPopupStyleFade;
  324. _dismissStyle = YMDismissStyleFade;
  325. _popDuration = YMPopupViewDefaultDuration;
  326. _dismissDuration = YMPopupViewDefaultDuration;
  327. _positionStyle = YMPositionStyleCenter;
  328. _adjustX = 0;
  329. _adjustY = 0;
  330. _isClickFeedback = NO;
  331. _isAvoidKeyboard = YES;
  332. _avoidKeyboardSpace = 10;
  333. _bgColor = [UIColor blackColor];
  334. _identifier = @"";
  335. _isShowKeyboard = NO;
  336. _showTime = 0.0;
  337. _priority = 0;
  338. _isHideBg = NO;
  339. _isShowKeyboard = NO;
  340. _isImpactFeedback = NO;
  341. _rectCorners = UIRectCornerAllCorners;
  342. _isSingle = NO;
  343. //拖拽相关属性初始化
  344. _dragStyle = YMDragStyleNO;
  345. _dragDismissStyle = YMDismissStyleNO;
  346. _dragDistance = 0.0f;
  347. _dragReboundTime = 0.25;
  348. _dragDismissDuration = YMPopupViewDefaultDuration;
  349. _swipeVelocity = 1000.0f;
  350. _isDragDismissStyle = NO;
  351. }
  352. + (nullable instancetype)initWithCustomView:(UIView *_Nonnull)customView {
  353. return [self initWithCustomView:customView popStyle:YMPopupStyleNO dismissStyle:YMDismissStyleNO];
  354. }
  355. + (nullable instancetype)initWithCustomView:(UIView *_Nonnull)customView
  356. popStyle:(YMPopupStyle)popStyle
  357. dismissStyle:(YMDismissStyle)dismissStyle {
  358. return [self initWithCustomView:customView
  359. parentView:nil
  360. popStyle:popStyle
  361. dismissStyle:dismissStyle];
  362. }
  363. + (nullable instancetype)initWithCustomView:(UIView *_Nonnull)customView
  364. parentView:(UIView *)parentView
  365. popStyle:(YMPopupStyle)popStyle
  366. dismissStyle:(YMDismissStyle)dismissStyle {
  367. // 检测自定义视图是否存在(check customView is exist)
  368. if (!customView) { return nil; }
  369. if (![parentView isKindOfClass:[UIView class]] && parentView != nil) {
  370. NSLog(@"parentView is error !!!");
  371. return nil;
  372. }
  373. CGRect popViewFrame = CGRectZero;
  374. if (parentView) {
  375. popViewFrame = parentView.bounds;
  376. } else {
  377. popViewFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  378. }
  379. YMPopupView *popView = [[YMPopupView alloc] initWithFrame:popViewFrame];
  380. popView.backgroundColor = [UIColor clearColor];
  381. popView.container = parentView? parentView : [UIApplication sharedApplication].keyWindow;
  382. popView.customView = customView;
  383. popView.backgroundView = [[YMPopupViewBgView alloc] initWithFrame:popView.bounds];
  384. popView.backgroundView.backgroundColor = [UIColor clearColor];
  385. popView.popStyle = popStyle;
  386. popView.dismissStyle = dismissStyle;
  387. [popView addSubview:popView.backgroundView];
  388. [popView.backgroundView addSubview:customView];
  389. //背景添加手势
  390. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:popView action:@selector(popViewBgViewTap:)];
  391. tap.delegate = popView;
  392. popView.tapGesture = tap;
  393. [popView.backgroundView addGestureRecognizer:tap];
  394. //添加拖拽手势
  395. popView.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:popView action:@selector(pan:)];
  396. popView.panGesture.delegate = popView;
  397. [popView.customView addGestureRecognizer:popView.panGesture];
  398. // [popView.backgroundView addTarget:popView action:@selector(popViewBgViewTap:) forControlEvents:UIControlEventTouchUpInside];;
  399. // UILongPressGestureRecognizer *customViewLP = [[UILongPressGestureRecognizer alloc] initWithTarget:popView action:@selector(bgLongPressEvent:)];
  400. // [popView.backgroundView addGestureRecognizer:customViewLP];
  401. // UITapGestureRecognizer *customViewTap = [[UITapGestureRecognizer alloc] initWithTarget:popView action:@selector(customViewClickEvent:)];
  402. // [popView.customView addGestureRecognizer:customViewTap];
  403. //键盘将要显示
  404. [[NSNotificationCenter defaultCenter] addObserver:popView
  405. selector:@selector(keyboardWillShow:)
  406. name:UIKeyboardWillShowNotification
  407. object:nil];
  408. //键盘显示完毕
  409. [[NSNotificationCenter defaultCenter] addObserver:popView
  410. selector:@selector(keyboardDidShow:)
  411. name:UIKeyboardDidShowNotification
  412. object:nil];
  413. //键盘frame将要改变
  414. [[NSNotificationCenter defaultCenter] addObserver:popView
  415. selector:@selector(keyboardWillChangeFrame:)
  416. name:UIKeyboardWillChangeFrameNotification
  417. object:nil];
  418. //键盘frame改变完毕
  419. [[NSNotificationCenter defaultCenter] addObserver:popView
  420. selector:@selector(keyboardDidChangeFrame:)
  421. name:UIKeyboardDidChangeFrameNotification
  422. object:nil];
  423. //键盘将要收起
  424. [[NSNotificationCenter defaultCenter] addObserver:popView
  425. selector:@selector(keyboardWillHide:)
  426. name:UIKeyboardWillHideNotification
  427. object:nil];
  428. //键盘收起完毕
  429. [[NSNotificationCenter defaultCenter] addObserver:popView
  430. selector:@selector(keyboardDidHide:)
  431. name:UIKeyboardDidHideNotification
  432. object:nil];
  433. //屏幕旋转
  434. [[NSNotificationCenter defaultCenter] addObserver:popView
  435. selector:@selector(statusBarOrientationChange:)
  436. name:UIApplicationDidChangeStatusBarOrientationNotification
  437. object:nil];
  438. //监听customView frame
  439. [popView.customView addObserver:popView
  440. forKeyPath:@"frame"
  441. options:NSKeyValueObservingOptionOld
  442. context:NULL];
  443. return popView;
  444. }
  445. - (void)dealloc {
  446. [self.customView removeObserver:self forKeyPath:@"frame"];
  447. [[NSNotificationCenter defaultCenter] removeObserver:self];
  448. //从待移除队列中销毁
  449. [YMPopupViewManager destroyInRemoveQueue:self];
  450. [self popupViewDidDismissForPopView:self];
  451. for (YmVoidBlock block in _popViewDidDismissBlocks.copy) {
  452. block ? block() : nil;
  453. }
  454. [_popViewDidDismissBlocks removeAllObjects];
  455. }
  456. - (void)removeFromSuperview {
  457. [super removeFromSuperview];
  458. [YMPopupViewManager transferredToRemoveQueueWithPopView:self];
  459. }
  460. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  461. UIView *hitView = [super hitTest:point withEvent:event];
  462. if (hitView != self.customView) {
  463. if(hitView == self && self.isHideBg){ return nil; }
  464. }
  465. return hitView;
  466. }
  467. #pragma mark - ***** 代理方法 *****
  468. - (void)popupViewBgClickForPopView:(YMPopupView *)popView {
  469. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  470. if ([delegate respondsToSelector:_cmd]) {
  471. [delegate popupViewBgClickForPopView:popView];
  472. }
  473. }
  474. }
  475. - (void)popupViewBgLongPressForPopView:(YMPopupView *)popView {
  476. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  477. if ([delegate respondsToSelector:_cmd]) {
  478. [delegate popupViewBgLongPressForPopView:popView];
  479. }
  480. }
  481. }
  482. - (void)popupViewWillPopForPopView:(YMPopupView *)popView {
  483. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  484. if ([delegate respondsToSelector:_cmd]) {
  485. [delegate popupViewWillPopForPopView:popView];
  486. }
  487. }
  488. }
  489. - (void)popupViewDidPopForPopView:(YMPopupView *)popView {
  490. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  491. if ([delegate respondsToSelector:_cmd]) {
  492. [delegate popupViewDidPopForPopView:popView];
  493. }
  494. }
  495. }
  496. - (void)popupViewCountDownForPopView:(YMPopupView *)popView forCountDown:(NSTimeInterval)timeInterval {
  497. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  498. if ([delegate respondsToSelector:_cmd]) {
  499. [delegate popupViewCountDownForPopView:popView forCountDown:timeInterval];
  500. }
  501. }
  502. }
  503. - (void)popupViewCountDownFinishForPopView:(YMPopupView *)popView {
  504. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  505. if ([delegate respondsToSelector:_cmd]) {
  506. [delegate popupViewCountDownFinishForPopView:popView];
  507. }
  508. }
  509. }
  510. - (void)popupViewWillDismissForPopView:(YMPopupView *)popView {
  511. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  512. if ([delegate respondsToSelector:_cmd]) {
  513. [delegate popupViewWillDismissForPopView:popView];
  514. }
  515. }
  516. }
  517. - (void)popupViewDidDismissForPopView:(YMPopupView *)popView {
  518. for (id<YMPopupViewProtocol> delegate in _delegates.copy) {
  519. if ([delegate respondsToSelector:_cmd]) {
  520. [delegate popupViewDidDismissForPopView:popView];
  521. }
  522. }
  523. }
  524. #pragma mark - ***** 界面布局 *****
  525. - (void)setCustomViewFrameWithHeight:(CGFloat)height {
  526. CGFloat changeY = 0;
  527. switch (self.positionStyle) {
  528. case YMPositionStyleTop ://贴顶
  529. {
  530. self.customView.ym_X = _backgroundView.ym_CenterX - _customView.ym_Width*0.5 + _adjustX;
  531. self.customView.ym_Y = _adjustY;
  532. changeY = _adjustY;
  533. }
  534. break;
  535. case YMPositionStyleLeft ://贴左
  536. {
  537. self.customView.ym_X = _adjustX;
  538. self.customView.ym_Y = _backgroundView.center.y - _customView.ym_Height*0.5 + _adjustY;
  539. changeY = height*0.5;
  540. }
  541. break;
  542. case YMPositionStyleBottom ://贴底
  543. {
  544. self.customView.ym_X = _backgroundView.ym_CenterX - _customView.ym_Width*0.5 + _adjustX;
  545. [self.customView layoutIfNeeded];
  546. self.customView.ym_Y = _backgroundView.ym_Height - _customView.ym_Height + _adjustY;
  547. changeY = height;
  548. }
  549. break;
  550. case YMPositionStyleRight ://贴右
  551. {
  552. self.customView.ym_X = _backgroundView.ym_Width - _customView.ym_Width + _adjustX;
  553. self.customView.ym_Y = _backgroundView.center.y - _customView.ym_Height*0.5 + _adjustY;
  554. changeY = height*0.5;
  555. }
  556. break;
  557. case YMPositionStyleTopLeft :///贴顶和左
  558. {
  559. self.customView.ym_X = _adjustX;
  560. self.customView.ym_Y = _adjustY;
  561. changeY = _adjustY;
  562. }
  563. break;
  564. case YMPositionStyleBottomLeft ://贴底和左
  565. {
  566. self.customView.ym_X = _adjustX;
  567. [self.customView layoutIfNeeded];
  568. self.customView.ym_Y = _backgroundView.ym_Height - _customView.ym_Height + _adjustY;
  569. changeY = height;
  570. }
  571. break;
  572. case YMPositionStyleBottomRight ://贴底和右
  573. {
  574. self.customView.ym_X = _backgroundView.ym_Width - _customView.ym_Width + _adjustX;
  575. [self.customView layoutIfNeeded];
  576. self.customView.ym_Y = _backgroundView.ym_Height - _customView.ym_Height + _adjustY;
  577. changeY = height;
  578. }
  579. break;
  580. case YMPositionStyleTopRight ://贴顶和右
  581. {
  582. self.customView.ym_X = _backgroundView.ym_Width - _customView.ym_Width + _adjustX;
  583. self.customView.ym_Y = _adjustY;
  584. changeY = _adjustY;
  585. }
  586. break;
  587. default://居中
  588. {
  589. self.customView.ym_X = _backgroundView.ym_CenterX - _customView.ym_Width*0.5 + _adjustX;
  590. self.customView.ym_Y = _backgroundView.center.y - _customView.ym_Height*0.5 + _adjustY;
  591. changeY = height*0.5;
  592. }
  593. break;
  594. }
  595. CGFloat originBottom = _originFrame.origin.y + _originFrame.size.height + _avoidKeyboardSpace;
  596. if (_isShowKeyboard && (originBottom > _keyboardY)) {//键盘已经显示
  597. CGFloat Y = _keyboardY - _customView.ym_Height - _avoidKeyboardSpace;
  598. _customView.ym_Y = Y;
  599. CGRect newFrame = _originFrame;
  600. newFrame.origin.y = newFrame.origin.y - changeY;
  601. newFrame.size = CGSizeMake(_originFrame.size.width, _customView.ym_Height);
  602. self.originFrame = newFrame;
  603. }else {//没有键盘显示
  604. self.originFrame = _customView.frame;
  605. }
  606. [self setCustomViewCorners];
  607. }
  608. - (void)setCustomViewCorners {
  609. BOOL isSetCorner = NO;
  610. if (self.rectCorners & UIRectCornerTopLeft) {
  611. isSetCorner = YES;
  612. }
  613. if (self.rectCorners & UIRectCornerTopRight) {
  614. isSetCorner = YES;
  615. }
  616. if (self.rectCorners & UIRectCornerBottomLeft) {
  617. isSetCorner = YES;
  618. }
  619. if (self.rectCorners & UIRectCornerBottomRight) {
  620. isSetCorner = YES;
  621. }
  622. if (self.rectCorners & UIRectCornerAllCorners) {
  623. isSetCorner = YES;
  624. }
  625. if (isSetCorner && self.cornerRadius>0) {
  626. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.customView.bounds byRoundingCorners:self.rectCorners cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
  627. CAShapeLayer * layer = [[CAShapeLayer alloc]init];
  628. layer.frame = self.customView.bounds;
  629. layer.path = path.CGPath;
  630. self.customView.layer.mask = layer;
  631. }
  632. }
  633. #pragma mark - ***** setter 设置器/数据处理 *****
  634. - (void)setPopDuration:(NSTimeInterval)popDuration {
  635. if (popDuration <= 0.0) { return; }
  636. _popDuration = popDuration;
  637. }
  638. - (void)setDismissDuration:(NSTimeInterval)dismissDuration {
  639. if (dismissDuration <= 0.0) { return; }
  640. _dismissDuration = dismissDuration;
  641. }
  642. - (void)setDragDismissDuration:(NSTimeInterval)dragDismissDuration {
  643. if (dragDismissDuration <= 0.0) { return; }
  644. _dragDismissDuration = dragDismissDuration;
  645. }
  646. - (void)setPositionStyle:(YMPositionStyle)positionStyle{
  647. _positionStyle = positionStyle;
  648. }
  649. - (void)setAdjustX:(CGFloat)adjustX {
  650. _adjustX = adjustX;
  651. // self.customView.x = _customView.x + adjustX;
  652. // self.originFrame = _customView.frame;
  653. [self setCustomViewFrameWithHeight:0];
  654. }
  655. - (void)setAdjustY:(CGFloat)adjustY {
  656. _adjustY = adjustY;
  657. // self.customView.y = _customView.y + adjustY;
  658. // self.originFrame = _customView.frame;
  659. [self setCustomViewFrameWithHeight:0];
  660. }
  661. - (void)setIsObserverScreenRotation:(BOOL)isObserverScreenRotation {
  662. _isObserverScreenRotation = isObserverScreenRotation;
  663. }
  664. - (void)setBgAlpha:(CGFloat)bgAlpha {
  665. _bgAlpha = (bgAlpha <= 0.0f) ? 0.0f : ((bgAlpha > 1.0) ? 1.0 : bgAlpha);
  666. }
  667. - (void)setIsClickFeedback:(BOOL)isClickFeedback {
  668. _isClickFeedback = isClickFeedback;
  669. }
  670. - (void)setIsHideBg:(BOOL)isHideBg {
  671. _isHideBg = isHideBg;
  672. self.backgroundView.isHideBg = isHideBg;
  673. self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  674. }
  675. - (void)setShowTime:(NSTimeInterval)showTime {
  676. _showTime = showTime;
  677. }
  678. - (void)setDelegate:(id<YMPopupViewProtocol>)delegate {
  679. if ([self.delegates containsObject:delegate]) return;
  680. [self.delegates addObject:delegate];
  681. }
  682. - (void)setDragDismissStyle:(YMDismissStyle)dragDismissStyle {
  683. _dragDismissStyle = dragDismissStyle;
  684. self.isDragDismissStyle = YES;
  685. }
  686. - (void)setPopViewDidDismissBlock:(void (^)(void))popViewDidDismissBlock
  687. {
  688. if (popViewDidDismissBlock) {
  689. [self.popViewDidDismissBlocks addObject:popViewDidDismissBlock];
  690. }
  691. }
  692. #pragma mark - ***** pop 弹出 *****
  693. - (void)pop {
  694. [self popWithStyle:self.popStyle duration:self.popDuration];
  695. }
  696. - (void)popWithStyle:(YMPopupStyle)popStyle {
  697. [self popWithStyle:popStyle duration:self.popDuration];
  698. }
  699. - (void)popWithDuration:(NSTimeInterval)duration {
  700. [self popWithStyle:self.popStyle duration:duration];
  701. }
  702. - (void)popWithStyle:(YMPopupStyle)popStyle duration:(NSTimeInterval)duration {
  703. [self popWithPopStyle:popStyle duration:duration isOutStack:NO];
  704. }
  705. - (void)popWithPopStyle:(YMPopupStyle)popStyle duration:(NSTimeInterval)duration isOutStack:(BOOL)isOutStack {
  706. NSTimeInterval resDuration = [self getPopDuration:duration];
  707. [self setCustomViewFrameWithHeight:0];
  708. self.originFrame = self.customView.frame;
  709. BOOL startTimer = NO;
  710. if (self.isSingle) {//单显
  711. NSArray *popViewArr = [YMPopupViewManager getAllPopViewForPopView:self];
  712. for (id obj in popViewArr) {//移除所有popView和移除定a时器
  713. YMPopupView *lastPopView = (YMPopupView *)obj;
  714. [lastPopView dismissWithDismissStyle:YMDismissStyleNO duration:0.2 isRemove:YES];
  715. }
  716. startTimer = YES;
  717. }else {//多显
  718. if (!isOutStack) {//处理隐藏倒数第二个popView
  719. NSArray *popViewArr = [YMPopupViewManager getAllPopViewForPopView:self];
  720. if (popViewArr.count >= 1) {
  721. id obj = popViewArr[popViewArr.count - 1];
  722. YMPopupView *lastPopView = (YMPopupView *)obj;
  723. if (self.isStack) {//堆叠显示
  724. startTimer = YES;
  725. }else {
  726. if (self.priority >= lastPopView.priority) {//置顶显示
  727. if (lastPopView.isShowKeyboard) {
  728. dispatch_async(dispatch_get_main_queue(), ^{
  729. [lastPopView endEditing:YES];
  730. });
  731. }
  732. [lastPopView dismissWithDismissStyle:YMDismissStyleFade duration:0.2 isRemove:NO];
  733. startTimer = YES;
  734. } else {//隐藏显示
  735. if (!self.parentView) {
  736. self.container = [UIApplication sharedApplication].keyWindow;
  737. }
  738. [YMPopupViewManager savePopView:self];
  739. return;
  740. }
  741. }
  742. } else {
  743. startTimer = YES;
  744. }
  745. }
  746. }
  747. // if (!isOutStack){
  748. // [self.parentView addSubview:self];
  749. // }
  750. if (!self.superview) {
  751. [self.container addSubview:self];
  752. [YMPopupViewM().showList addPointer:(__bridge void * _Nullable)self];
  753. }
  754. if (!isOutStack){
  755. [self popupViewWillPopForPopView:self];
  756. self.popViewWillPopBlock? self.popViewWillPopBlock():nil;
  757. }
  758. //动画处理
  759. [self popAnimationWithPopStyle:popStyle duration:resDuration];
  760. //震动反馈
  761. [self beginImpactFeedback];
  762. YMPopupViewWK(self);
  763. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(resDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  764. if (!isOutStack){
  765. [self popupViewDidPopForPopView:self];
  766. wk_self.popViewDidPopBlock? wk_self.popViewDidPopBlock():nil;
  767. }
  768. if (startTimer){ [self startTimer]; }
  769. });
  770. //保存popView
  771. if (!isOutStack) { [YMPopupViewManager savePopView:self]; }
  772. }
  773. - (void)popAnimationWithPopStyle:(YMPopupStyle)popStyle duration:(NSTimeInterval)duration {
  774. YMPopupViewWK(self);
  775. if (popStyle == YMPopupStyleFade) {//渐变出现
  776. self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  777. self.customView.alpha = 0.0f;
  778. [UIView animateWithDuration:0.2 animations:^{
  779. self.backgroundView.backgroundColor =[self getNewColorWith:self.bgColor alpha:self.bgAlpha];
  780. self.customView.alpha = 1.0f;
  781. }];
  782. } else if (popStyle == YMPopupStyleNO){//无动画
  783. self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  784. [UIView animateWithDuration:0.1 animations:^{
  785. self.backgroundView.backgroundColor =[self getNewColorWith:self.bgColor alpha:self.bgAlpha];
  786. }];
  787. self.customView.alpha = 1.0f;
  788. } else {//有动画
  789. self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  790. [UIView animateWithDuration:duration animations:^{
  791. wk_self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:self.bgAlpha];
  792. }];
  793. //自定义view过渡动画
  794. [self hanlePopAnimationWithDuration:duration popStyle:popStyle];
  795. }
  796. }
  797. - (void)hanlePopAnimationWithDuration:(NSTimeInterval)duration
  798. popStyle:(YMPopupStyle)popStyle {
  799. self.alpha = 0;
  800. [UIView animateWithDuration:duration*0.2 animations:^{
  801. self.alpha = 1;
  802. }];
  803. YMPopupViewWK(self);
  804. switch (popStyle) {
  805. case YMPopupStyleScale:// < 缩放动画,先放大,后恢复至原大小
  806. {
  807. [self animationWithLayer:_customView.layer duration:((0.3*duration)/0.7) values:@[@0.0, @1.2, @1.0]]; // 另外一组动画值(the other animation values) @[@0.0, @1.2, @0.9, @1.0]
  808. }
  809. break;
  810. case YMPopupStyleTop:
  811. {
  812. CGPoint startPosition = self.customView.layer.position;
  813. self.customView.layer.position = CGPointMake(startPosition.x, -(_customView.ym_Height*0.5) + _adjustY);
  814. [UIView animateWithDuration:duration
  815. delay:0
  816. usingSpringWithDamping:1.0
  817. initialSpringVelocity:1.0
  818. options:UIViewAnimationOptionCurveEaseOut
  819. animations:^{
  820. wk_self.customView.layer.position = startPosition;
  821. } completion:nil];
  822. }
  823. break;
  824. case YMPopupStyleSmoothFromTop:
  825. case YMPopupStyleSmoothFromBottom:
  826. case YMPopupStyleSmoothFromLeft:
  827. case YMPopupStyleSmoothFromRight:
  828. case YMPopupStyleSpringFromTop:
  829. case YMPopupStyleSpringFromLeft:
  830. case YMPopupStyleSpringFromBottom:
  831. case YMPopupStyleSpringFromRight:
  832. {
  833. CGPoint startPosition = self.customView.layer.position;
  834. if (popStyle == YMPopupStyleSmoothFromTop || popStyle == YMPopupStyleSpringFromTop) {
  835. self.customView.layer.position = CGPointMake(startPosition.x, -_customView.ym_Height*0.5);
  836. } else if (popStyle == YMPopupStyleSmoothFromLeft || popStyle == YMPopupStyleSpringFromLeft) {
  837. self.customView.layer.position = CGPointMake(-_customView.ym_Width*0.5, startPosition.y);
  838. } else if (popStyle == YMPopupStyleSmoothFromBottom || popStyle == YMPopupStyleSpringFromBottom) {
  839. self.customView.layer.position = CGPointMake(startPosition.x, CGRectGetMaxY(self.frame) + _customView.ym_Height*0.5);
  840. } else {
  841. self.customView.layer.position = CGPointMake(CGRectGetMaxX(self.frame) + _customView.ym_Width*0.5 , startPosition.y);
  842. }
  843. CGFloat damping = 1.0;
  844. if (popStyle == YMPopupStyleSpringFromTop||
  845. popStyle == YMPopupStyleSpringFromLeft||
  846. popStyle == YMPopupStyleSpringFromBottom||
  847. popStyle == YMPopupStyleSpringFromRight) {
  848. damping = 0.65;
  849. }
  850. [UIView animateWithDuration:duration
  851. delay:0
  852. usingSpringWithDamping:damping
  853. initialSpringVelocity:1.0
  854. options:UIViewAnimationOptionCurveEaseOut
  855. animations:^{
  856. wk_self.customView.layer.position = startPosition;
  857. } completion:nil];
  858. }
  859. break;
  860. case YMPopupStyleCardDropFromLeft:
  861. case YMPopupStyleCardDropFromRight:
  862. {
  863. CGPoint startPosition = self.customView.layer.position;
  864. if (popStyle == YMPopupStyleCardDropFromLeft) {
  865. self.customView.layer.position = CGPointMake(startPosition.x * 1.0, -_customView.ym_Height*0.5);
  866. self.customView.transform = CGAffineTransformMakeRotation(LST_DEGREES_TO_RADIANS(15.0));
  867. } else {
  868. self.customView.layer.position = CGPointMake(startPosition.x * 1.0, -_customView.ym_Height*0.5);
  869. self.customView.transform = CGAffineTransformMakeRotation(LST_DEGREES_TO_RADIANS(-15.0));
  870. }
  871. [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.75 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  872. wk_self.customView.layer.position = startPosition;
  873. } completion:nil];
  874. [UIView animateWithDuration:duration*0.6 animations:^{
  875. wk_self.customView.layer.transform = CATransform3DMakeRotation(LST_DEGREES_TO_RADIANS((popStyle == YMPopupStyleCardDropFromRight) ? 5.5 : -5.5), 0, 0, 0);
  876. } completion:^(BOOL finished) {
  877. [UIView animateWithDuration:duration*0.2 animations:^{
  878. wk_self.customView.transform = CGAffineTransformMakeRotation(LST_DEGREES_TO_RADIANS((popStyle == YMPopupStyleCardDropFromRight) ? -1.0 : 1.0));
  879. } completion:^(BOOL finished) {
  880. [UIView animateWithDuration:duration*0.2 animations:^{
  881. wk_self.customView.transform = CGAffineTransformMakeRotation(0.0);
  882. } completion:nil];
  883. }];
  884. }];
  885. }
  886. break;
  887. default:
  888. break;
  889. }
  890. }
  891. #pragma mark - ***** dismiss 移除 *****
  892. - (void)dismiss {
  893. [self dismissWithStyle:self.dismissStyle duration:self.dismissDuration];
  894. }
  895. - (void)dismissWithStyle:(YMDismissStyle)dismissStyle {
  896. [self dismissWithStyle:dismissStyle duration:self.dismissDuration];
  897. }
  898. - (void)dismissWithDuration:(NSTimeInterval)duration {
  899. [self dismissWithStyle:self.dismissStyle duration:duration];
  900. }
  901. - (void)dismissWithStyle:(YMDismissStyle)dismissStyle duration:(NSTimeInterval)duration {
  902. [self dismissWithDismissStyle:dismissStyle duration:duration isRemove:YES];
  903. }
  904. - (void)dismissWithDismissStyle:(YMDismissStyle)dismissStyle
  905. duration:(NSTimeInterval)duration
  906. isRemove:(BOOL)isRemove {
  907. NSTimeInterval resDuration = [self getDismissDuration:duration];
  908. if (isRemove) {
  909. //把当前popView转移到待移除队列 避免线程安全问题
  910. [YMPopupViewManager transferredToRemoveQueueWithPopView:self];
  911. }
  912. [self popupViewWillDismissForPopView:self];
  913. self.popViewWillDismissBlock? self.popViewWillDismissBlock():nil;
  914. YMPopupViewWK(self)
  915. [self dismissAnimationWithDismissStyle:dismissStyle duration:resDuration];
  916. if (!self.isSingle && (isRemove && [YMPopupViewManager getAllPopViewForPopView:self].count >= 1)){//多显
  917. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(resDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  918. //popView出栈
  919. if (!self.isStack && [YMPopupViewManager getAllPopViewForPopView:self].count >= 1) {
  920. NSArray *popViewArr = [YMPopupViewManager getAllPopViewForPopView:self];
  921. id obj = popViewArr[popViewArr.count-1];
  922. YMPopupView *tPopView = (YMPopupView *)obj;
  923. if (!tPopView.superview) {
  924. [tPopView.container addSubview:tPopView];
  925. [YMPopupViewM().showList addPointer:(__bridge void * _Nullable)tPopView];
  926. }
  927. [tPopView popWithPopStyle:YMPopupStyleFade duration:0.25 isOutStack:YES];
  928. }
  929. });
  930. }
  931. if (isRemove) {
  932. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(resDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  933. [wk_self removeFromSuperview];
  934. });
  935. }
  936. }
  937. - (void)dismissAnimationWithDismissStyle:(YMDismissStyle)dismissStyle duration:(NSTimeInterval)duration {
  938. YMPopupViewWK(self);
  939. if (dismissStyle == YMPopupStyleFade) {
  940. [UIView animateWithDuration:0.2 animations:^{
  941. wk_self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  942. wk_self.customView.alpha = 0.0f;
  943. }];
  944. }else if (dismissStyle == YMPopupStyleNO){
  945. wk_self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  946. wk_self.customView.alpha = 0.0f;
  947. }else {//有动画
  948. [UIView animateWithDuration:duration*0.8 animations:^{
  949. wk_self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  950. }];
  951. [self hanleDismissAnimationWithDuration:duration withDismissStyle:dismissStyle];
  952. }
  953. }
  954. - (void)hanleDismissAnimationWithDuration:(NSTimeInterval)duration
  955. withDismissStyle:(YMDismissStyle)dismissStyle {
  956. [UIView animateWithDuration:duration*0.8 animations:^{
  957. self.alpha = 0;
  958. }];
  959. YMPopupViewWK(self);;
  960. switch (dismissStyle) {
  961. case YMDismissStyleScale:
  962. {
  963. [self animationWithLayer:self.customView.layer duration:((0.2*duration)/0.8) values:@[@1.0, @0.66, @0.33, @0.01]];
  964. }
  965. break;
  966. case YMDismissStyleTop:
  967. {
  968. CGPoint startPosition = self.customView.layer.position;
  969. CGPoint endPosition = CGPointMake(startPosition.x, -startPosition.y);
  970. [UIView animateWithDuration:duration*0.2 animations:^{
  971. wk_self.customView.layer.position = CGPointMake(startPosition.x, startPosition.y + self->_adjustY);
  972. } completion:^(BOOL finished) {
  973. [UIView animateWithDuration:duration*0.8 animations:^{
  974. wk_self.customView.layer.position = endPosition;
  975. } completion:nil];
  976. }];
  977. }
  978. break;
  979. case YMDismissStyleSmoothToTop:
  980. case YMDismissStyleSmoothToBottom:
  981. case YMDismissStyleSmoothToLeft:
  982. case YMDismissStyleSmoothToRight:
  983. {
  984. CGPoint startPosition = self.customView.layer.position;
  985. CGPoint endPosition = self.customView.layer.position;
  986. if (dismissStyle == YMDismissStyleSmoothToTop) {
  987. endPosition = CGPointMake(startPosition.x, -(_customView.ym_Height*0.5));
  988. } else if (dismissStyle == YMDismissStyleSmoothToBottom) {
  989. endPosition = CGPointMake(startPosition.x, CGRectGetMaxY(self.frame) + _customView.ym_Height*0.5);
  990. } else if (dismissStyle == YMDismissStyleSmoothToLeft) {
  991. endPosition = CGPointMake(-_customView.ym_Width*0.5, startPosition.y);
  992. } else {
  993. endPosition = CGPointMake(CGRectGetMaxX(self.frame) + _customView.ym_Width*0.5, startPosition.y);
  994. }
  995. [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  996. wk_self.customView.layer.position = endPosition;
  997. } completion:nil];
  998. }
  999. break;
  1000. case YMDismissStyleCardDropToLeft:
  1001. case YMDismissStyleCardDropToRight:
  1002. {
  1003. CGPoint startPosition = self.customView.layer.position;
  1004. BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
  1005. __block CGFloat rotateEndY = 0.0f;
  1006. [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  1007. if (dismissStyle == YMDismissStyleCardDropToLeft) {
  1008. wk_self.customView.transform = CGAffineTransformMakeRotation(M_1_PI * 0.75);
  1009. if (isLandscape) rotateEndY = fabs(wk_self.customView.ym_Y);
  1010. wk_self.customView.layer.position = CGPointMake(startPosition.x, CGRectGetMaxY(wk_self.frame) + startPosition.y + rotateEndY);
  1011. } else {
  1012. wk_self.customView.transform = CGAffineTransformMakeRotation(-M_1_PI * 0.75);
  1013. if (isLandscape) rotateEndY = fabs(wk_self.customView.ym_Y);
  1014. wk_self.customView.layer.position = CGPointMake(startPosition.x * 1.25, CGRectGetMaxY(wk_self.frame) + startPosition.y + rotateEndY);
  1015. }
  1016. } completion:nil];
  1017. }
  1018. break;
  1019. case YMDismissStyleCardDropToTop:
  1020. {
  1021. CGPoint startPosition = self.customView.layer.position;
  1022. CGPoint endPosition = CGPointMake(startPosition.x, -startPosition.y);
  1023. [UIView animateWithDuration:duration*0.2 animations:^{
  1024. wk_self.customView.layer.position = CGPointMake(startPosition.x, startPosition.y + 50.0f);
  1025. } completion:^(BOOL finished) {
  1026. [UIView animateWithDuration:duration*0.8 animations:^{
  1027. wk_self.customView.layer.position = endPosition;
  1028. } completion:nil];
  1029. }];
  1030. }
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. }
  1036. #pragma mark - ***** other 其他 *****
  1037. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  1038. if([keyPath isEqualToString:@"frame"]) {
  1039. CGRect newFrame = CGRectNull;
  1040. CGRect oldFrame = CGRectNull;
  1041. if([object valueForKeyPath:keyPath] != [NSNull null]) {
  1042. //此处为获取新的frame
  1043. newFrame = [[object valueForKeyPath:keyPath] CGRectValue];
  1044. oldFrame = [[change valueForKeyPath:@"old"] CGRectValue];
  1045. if (!CGSizeEqualToSize(newFrame.size, oldFrame.size)) {
  1046. [self setCustomViewFrameWithHeight:newFrame.size.height - oldFrame.size.height];
  1047. }
  1048. }
  1049. }else {
  1050. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  1051. }
  1052. }
  1053. //按钮的压下事件 按钮缩小
  1054. - (void)bgLongPressEvent:(UIGestureRecognizer *)ges {
  1055. self.bgLongPressBlock? self.bgLongPressBlock():nil;
  1056. }
  1057. - (void)customViewClickEvent:(UIGestureRecognizer *)ges {
  1058. if (self.isClickFeedback) {
  1059. CGFloat scale = 0.95;
  1060. [UIView animateWithDuration:0.25 animations:^{
  1061. ges.view.transform = CGAffineTransformMakeScale(scale, scale);
  1062. } completion:^(BOOL finished) {
  1063. [UIView animateWithDuration:0.25 animations:^{
  1064. ges.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
  1065. } completion:^(BOOL finished) {
  1066. }];
  1067. }];
  1068. }
  1069. }
  1070. - (void)popViewBgViewTap:(UIButton *)tap {
  1071. if (self.bgClickBlock) {
  1072. if (self.isShowKeyboard) {
  1073. dispatch_async(dispatch_get_main_queue(), ^{
  1074. [self endEditing:YES];
  1075. });
  1076. }
  1077. self.bgClickBlock();
  1078. }
  1079. if (_isClickBgDismiss) {
  1080. [self dismiss];
  1081. }
  1082. }
  1083. - (NSTimeInterval)getPopDuration:(NSTimeInterval)currDuration {
  1084. if (_popStyle == YMPopupStyleNO) { return 0.0f; }
  1085. if (_popStyle == YMPopupStyleFade) { return 0.2f; }
  1086. if (currDuration<=0) { self.popDuration = YMPopupViewDefaultDuration; }
  1087. if (self.popDuration == YMPopupViewDefaultDuration) {
  1088. NSTimeInterval defaultDuration = 0.0f;
  1089. if (_popStyle == YMPopupStyleScale) {
  1090. defaultDuration = 0.3f;
  1091. }
  1092. if (_popStyle == YMPopupStyleSmoothFromTop ||
  1093. _popStyle == YMPopupStyleSmoothFromLeft ||
  1094. _popStyle == YMPopupStyleSmoothFromBottom ||
  1095. _popStyle == YMPopupStyleSmoothFromRight ||
  1096. _popStyle == YMPopupStyleSpringFromTop ||
  1097. _popStyle == YMPopupStyleSpringFromLeft ||
  1098. _popStyle == YMPopupStyleSpringFromBottom ||
  1099. _popStyle == YMPopupStyleSpringFromRight ||
  1100. _popStyle == YMPopupStyleCardDropFromLeft ||
  1101. _popStyle == YMPopupStyleCardDropFromRight) {
  1102. defaultDuration = 0.6f;
  1103. }
  1104. return defaultDuration;
  1105. } else {
  1106. return currDuration;
  1107. }
  1108. }
  1109. - (NSTimeInterval)getDismissDuration:(NSTimeInterval)currDuration {
  1110. if (_dismissStyle == YMDismissStyleNO) { return 0.0f; }
  1111. if (_dismissStyle == YMDismissStyleFade) { return 0.2f; }
  1112. if (self.dismissDuration == YMPopupViewDefaultDuration) {
  1113. NSTimeInterval defaultDuration = 0.0f;
  1114. if (_dismissStyle == YMDismissStyleNO) {
  1115. defaultDuration = 0.0f;
  1116. }
  1117. if (_dismissStyle == YMDismissStyleScale) {
  1118. defaultDuration = 0.3f;
  1119. }
  1120. if (_dismissStyle == YMDismissStyleSmoothToTop ||
  1121. _dismissStyle == YMDismissStyleSmoothToBottom ||
  1122. _dismissStyle == YMDismissStyleSmoothToLeft ||
  1123. _dismissStyle == YMDismissStyleSmoothToRight ||
  1124. _dismissStyle == YMDismissStyleCardDropToLeft ||
  1125. _dismissStyle == YMDismissStyleCardDropToRight ||
  1126. _dismissStyle == YMDismissStyleCardDropToTop) {
  1127. defaultDuration = 0.5f;
  1128. }
  1129. return defaultDuration;
  1130. } else {
  1131. return currDuration;
  1132. }
  1133. }
  1134. - (NSTimeInterval)getDragDismissDuration {
  1135. if (self.dragDismissDuration == YMPopupViewDefaultDuration) {
  1136. return [self getDismissDuration:self.dismissDuration];
  1137. } else {
  1138. return self.dragDismissDuration;
  1139. }
  1140. }
  1141. - (void)animationWithLayer:(CALayer *)layer duration:(CGFloat)duration values:(NSArray *)values {
  1142. CAKeyframeAnimation *KFAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  1143. KFAnimation.duration = duration;
  1144. KFAnimation.removedOnCompletion = NO;
  1145. KFAnimation.fillMode = kCAFillModeForwards;
  1146. // KFAnimation.delegate = self;//造成强应用 popView释放不了
  1147. NSMutableArray *valueArr = [NSMutableArray arrayWithCapacity:values.count];
  1148. for (NSUInteger i = 0; i<values.count; i++) {
  1149. CGFloat scaleValue = [values[i] floatValue];
  1150. [valueArr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(scaleValue, scaleValue, scaleValue)]];
  1151. }
  1152. KFAnimation.values = valueArr;
  1153. KFAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  1154. [layer addAnimation:KFAnimation forKey:nil];
  1155. }
  1156. - (UIColor *)lst_BlackWithAlpha:(CGFloat)alpha {
  1157. return [UIColor colorWithRed:0 green:0 blue:0 alpha:alpha];
  1158. }
  1159. // 改变UIColor的Alpha
  1160. - (UIColor *)getNewColorWith:(UIColor *)color alpha:(CGFloat)alpha {
  1161. if (self.isHideBg) {
  1162. return UIColor.clearColor;
  1163. }
  1164. if (self.bgColor == UIColor.clearColor) {
  1165. return UIColor.clearColor;
  1166. }
  1167. CGFloat red = 0.0;
  1168. CGFloat green = 0.0;
  1169. CGFloat blue = 0.0;
  1170. CGFloat resAlpha = 0.0;
  1171. [color getRed:&red green:&green blue:&blue alpha:&resAlpha];
  1172. UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  1173. return newColor;
  1174. }
  1175. - (void)startTimer {
  1176. if (self.showTime > 0) {
  1177. YMPopupViewWK(self);;
  1178. NSString *idStr = [NSString stringWithFormat:@"YMPopupView_%p",self];
  1179. [YMTimer addMinuteTimerForTime:self.showTime identifier:idStr handle:^(NSString * day, NSString * hour, NSString * minute, NSString * second, NSString * ms) {
  1180. if (wk_self.popViewCountDownBlock) {
  1181. wk_self.popViewCountDownBlock(wk_self, [second doubleValue]);
  1182. }
  1183. [wk_self popupViewCountDownForPopView:wk_self forCountDown:[second doubleValue]];
  1184. } finish:^(NSString * identifier) {
  1185. [wk_self popupViewCountDownFinishForPopView:wk_self];
  1186. [self dismiss];
  1187. } pause:^(NSString * identifier) {}];
  1188. }
  1189. }
  1190. /** 删除指定代理 */
  1191. - (void)removeForDelegate:(id<YMPopupViewProtocol>)delegate {
  1192. if (delegate) {
  1193. if ([self.delegates containsObject:delegate]) {
  1194. [self.delegates removeObject:delegate];
  1195. }
  1196. }
  1197. }
  1198. /** 删除代理池 删除所有代理 */
  1199. - (void)removeAllDelegate {
  1200. if (self.delegates.count > 0) {
  1201. [self.delegates removeAllObjects];
  1202. }
  1203. }
  1204. - (void)beginImpactFeedback {
  1205. if (self.isImpactFeedback) {
  1206. if (@available(iOS 10.0, *)) {
  1207. UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
  1208. [feedBackGenertor impactOccurred];
  1209. }
  1210. }
  1211. }
  1212. #pragma mark - ***** 横竖屏改变 *****
  1213. - (void)statusBarOrientationChange:(NSNotification *)notification {
  1214. if (self.isObserverScreenRotation) {
  1215. CGRect originRect = self.customView.frame;
  1216. self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  1217. self.backgroundView.frame = self.bounds;
  1218. self.customView.frame = originRect;
  1219. [self setCustomViewFrameWithHeight:0];
  1220. }
  1221. }
  1222. #pragma mark - ***** 键盘弹出/收回 *****
  1223. - (void)keyboardWillShow:(NSNotification *)notification{
  1224. // LSTPVLog(@"keyboardWillShow");
  1225. _isShowKeyboard = YES;
  1226. self.keyboardWillShowBlock? self.keyboardWillShowBlock():nil;
  1227. if (!self.isAvoidKeyboard) { return; }
  1228. CGFloat customViewMaxY = self.customView.ym_Bottom + self.avoidKeyboardSpace;
  1229. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1230. CGRect keyboardEedFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  1231. CGFloat keyboardMaxY = keyboardEedFrame.origin.y;
  1232. self.isAvoidKeyboard = YES;
  1233. self.avoidKeyboardOffset = customViewMaxY - keyboardMaxY;
  1234. self.keyboardY = keyboardEedFrame.origin.y;
  1235. //键盘遮挡到弹窗
  1236. if ((keyboardMaxY<customViewMaxY) || ((_originFrame.origin.y + _customView.ym_Height) > keyboardMaxY)) {
  1237. //执行动画
  1238. [UIView animateWithDuration:duration animations:^{
  1239. self.customView.ym_Y = self.customView.ym_Y - self.avoidKeyboardOffset;
  1240. }];
  1241. }
  1242. }
  1243. - (void)keyboardDidShow:(NSNotification *)notification{
  1244. _isShowKeyboard = YES;
  1245. self.keyboardDidShowBlock? self.keyboardDidShowBlock():nil;
  1246. }
  1247. - (void)keyboardWillHide:(NSNotification *)notification{
  1248. _isShowKeyboard = NO;
  1249. self.keyboardWillHideBlock? self.keyboardWillHideBlock():nil;
  1250. if (!self.isAvoidKeyboard) {
  1251. return;
  1252. }
  1253. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1254. [UIView animateWithDuration:duration animations:^{
  1255. self.customView.ym_Y = self.originFrame.origin.y;
  1256. }];
  1257. }
  1258. - (void)keyboardDidHide:(NSNotification *)notification{
  1259. _isShowKeyboard = NO;
  1260. self.keyboardDidHideBlock? self.keyboardDidHideBlock():nil;
  1261. }
  1262. - (void)keyboardWillChangeFrame:(NSNotification *)notification{
  1263. // LSTPVLog(@"键盘frame将要改变");
  1264. if (self.keyboardFrameWillChangeBlock) {
  1265. CGRect keyboardBeginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  1266. CGRect keyboardEedFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  1267. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1268. self.keyboardFrameWillChangeBlock(keyboardBeginFrame,keyboardEedFrame,duration);
  1269. }
  1270. }
  1271. - (void)keyboardDidChangeFrame:(NSNotification *)notification{
  1272. // LSTPVLog(@"键盘frame已经改变");
  1273. if (self.keyboardFrameDidChangeBlock) {
  1274. CGRect keyboardBeginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  1275. CGRect keyboardEedFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  1276. CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1277. self.keyboardFrameDidChangeBlock(keyboardBeginFrame,keyboardEedFrame,duration);
  1278. }
  1279. }
  1280. #pragma mark - ***** UIGestureRecognizerDelegate *****
  1281. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  1282. if(gestureRecognizer == self.panGesture) {
  1283. UIView *touchView = touch.view;
  1284. while (touchView != nil) {
  1285. if([touchView isKindOfClass:[UIScrollView class]]) {
  1286. self.isDragScrollView = YES;
  1287. self.scrollerView = (UIScrollView *)touchView;
  1288. break;
  1289. } else if(touchView == self.customView) {
  1290. self.isDragScrollView = NO;
  1291. break;
  1292. }
  1293. touchView = (id)[touchView nextResponder];
  1294. }
  1295. }
  1296. return YES;
  1297. }
  1298. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  1299. if(gestureRecognizer == self.tapGesture) {
  1300. //如果是点击手势
  1301. CGPoint point = [gestureRecognizer locationInView:self.customView];
  1302. BOOL iscontain = [self.customView.layer containsPoint:point];
  1303. if(iscontain) {
  1304. return NO;
  1305. }
  1306. } else if(gestureRecognizer == self.panGesture){
  1307. //如果是自己加的拖拽手势
  1308. // LSTPVLog(@"gestureRecognizerShouldBegin");
  1309. }
  1310. return YES;
  1311. }
  1312. //3. 是否与其他手势共存,一般使用默认值(默认返回NO:不与任何手势共存)
  1313. - (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  1314. if(gestureRecognizer == self.panGesture) {
  1315. if ([otherGestureRecognizer isKindOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")] || [otherGestureRecognizer isKindOfClass:NSClassFromString(@"UIPanGestureRecognizer")] ) {
  1316. if([otherGestureRecognizer.view isKindOfClass:[UIScrollView class]] ) {
  1317. return YES;
  1318. }
  1319. }
  1320. }
  1321. return NO;
  1322. }
  1323. //拖拽手势
  1324. - (void)pan:(UIPanGestureRecognizer *)panGestureRecognizer {
  1325. self.panOffsetBlock?self.panOffsetBlock(CGPointMake(_customView.ym_X-_originFrame.origin.x, _customView.ym_Y-_originFrame.origin.y)):nil;
  1326. if (self.dragStyle == YMDragStyleNO) {return;}
  1327. // 获取手指的偏移量
  1328. CGPoint transP = [panGestureRecognizer translationInView:self.customView];
  1329. CGPoint velocity = [panGestureRecognizer velocityInView:[UIApplication sharedApplication].keyWindow];
  1330. if(self.isDragScrollView) {//含有tableView,collectionView,scrollView
  1331. //如果当前拖拽的是tableView
  1332. if(self.scrollerView.contentOffset.y <= 0) {
  1333. //如果tableView置于顶端
  1334. if(transP.y > 0) {
  1335. //如果向下拖拽
  1336. self.scrollerView.contentOffset = CGPointMake(0, 0);
  1337. self.scrollerView.panGestureRecognizer.enabled = NO;
  1338. self.isDragScrollView = NO;
  1339. //向下拖
  1340. self.customView.frame = CGRectMake(_customView.ym_X, _customView.ym_Y + transP.y, _customView.ym_Width, _customView.ym_Height);
  1341. } else {
  1342. //如果向上拖拽
  1343. }
  1344. }
  1345. } else {//不含有tableView,collectionView,scrollView
  1346. CGFloat customViewX = self.customView.ym_X;
  1347. CGFloat customViewY = self.customView.ym_Y;
  1348. //X正方向移动
  1349. if ((self.dragStyle & YMDragStyleX_Positive) && (customViewX >= _originFrame.origin.x)) {
  1350. if (transP.x > 0) {
  1351. customViewX = customViewX + transP.x;
  1352. } else if(transP.x < 0 && customViewX > _originFrame.origin.x ){
  1353. customViewX = (customViewX + transP.x) > _originFrame.origin.x? (customViewX + transP.x):(_originFrame.origin.x);
  1354. }
  1355. }
  1356. //X负方向移动
  1357. if ((self.dragStyle & YMDragStyleX_Negative) && (customViewX <= self.originFrame.origin.x)) {
  1358. if (transP.x < 0) {
  1359. customViewX = customViewX + transP.x;
  1360. } else if(transP.x > 0 && customViewX < _originFrame.origin.x ){
  1361. customViewX = (customViewX + transP.x) < _originFrame.origin.x? (customViewX + transP.x):(_originFrame.origin.x);
  1362. }
  1363. }
  1364. //Y正方向移动
  1365. if (self.dragStyle & YMDragStyleY_Positive && (customViewY >= _originFrame.origin.y)) {
  1366. if (transP.y > 0) {
  1367. customViewY = customViewY + transP.y;
  1368. } else if(transP.y < 0 && customViewY > _originFrame.origin.y ){
  1369. customViewY = (customViewY + transP.y) > _originFrame.origin.y ?(customViewY + transP.y):(_originFrame.origin.y);
  1370. }
  1371. }
  1372. //Y负方向移动
  1373. if (self.dragStyle & YMDragStyleY_Negative&&(customViewY <= _originFrame.origin.y)) {
  1374. if (transP.y < 0) {
  1375. customViewY = customViewY + transP.y;
  1376. } else if(transP.y > 0 && customViewY < _originFrame.origin.y){
  1377. customViewY = (customViewY + transP.y) < _originFrame.origin.y?(customViewY + transP.y):(_originFrame.origin.y);
  1378. }
  1379. }
  1380. self.customView.frame = CGRectMake(customViewX, customViewY, _customView.ym_Width, _customView.ym_Height);
  1381. }
  1382. [panGestureRecognizer setTranslation:CGPointZero inView:self.customView];
  1383. if(panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
  1384. if (self.scrollerView) {
  1385. self.scrollerView.panGestureRecognizer.enabled = YES;
  1386. }
  1387. YMPopupViewWK(self)
  1388. //拖拽松开回位Block
  1389. void (^dragReboundBlock)(void) = ^ {
  1390. [UIView animateWithDuration:wk_self.dragReboundTime
  1391. delay:0.1f
  1392. options:UIViewAnimationOptionCurveEaseOut
  1393. animations:^{
  1394. CGRect frame = wk_self.customView.frame;
  1395. frame.origin.y = wk_self.frame.size.height - wk_self.customView.frame.size.height;
  1396. wk_self.customView.frame = wk_self.originFrame;
  1397. } completion:^(BOOL finished) {}];
  1398. };
  1399. //横扫移除Block
  1400. void (^sweepBlock)(BOOL,BOOL,BOOL,BOOL) = ^(BOOL isX_P, BOOL isX_N, BOOL isY_P, BOOL isY_N) {
  1401. if (isX_P==NO && isX_N==NO && isY_P==NO && isY_N==NO) {
  1402. dragReboundBlock();
  1403. return;
  1404. }
  1405. if (isY_P==NO && isY_N==NO && self.sweepDismissStyle == YMSweepDismissStyleSmooth) {//X轴可轻扫
  1406. if (velocity.x>0) {//正向
  1407. [self dismissWithStyle:YMDismissStyleSmoothToRight duration:self.dismissDuration];
  1408. } else {//负向
  1409. [self dismissWithStyle:YMDismissStyleSmoothToLeft duration:self.dismissDuration];
  1410. }
  1411. return;
  1412. }
  1413. if (isX_P==NO && isX_N==NO && self.sweepDismissStyle == YMSweepDismissStyleSmooth) {//Y轴可轻扫
  1414. if (velocity.y>0) {//正向
  1415. [self dismissWithStyle:YMDismissStyleSmoothToBottom duration:self.dismissDuration];
  1416. } else {//负向
  1417. [self dismissWithStyle:YMDismissStyleSmoothToTop duration:self.dismissDuration];
  1418. }
  1419. return;
  1420. }
  1421. // 移除,以手势速度飞出
  1422. [UIView animateWithDuration:0.5 animations:^{
  1423. wk_self.backgroundView.backgroundColor = [self getNewColorWith:self.bgColor alpha:0.0];
  1424. self.customView.center = CGPointMake(isX_P || isX_N?velocity.x:self.customView.ym_CenterX, isY_P||isY_N?velocity.y:self.customView.center.y);
  1425. } completion:^(BOOL finished) {
  1426. [wk_self dismissWithStyle:YMDismissStyleFade duration:0.1];
  1427. }];
  1428. };
  1429. double velocityX = sqrt(pow(velocity.x, 2));
  1430. double velocityY = sqrt(pow(velocity.y, 2));
  1431. if((velocityX >= self.swipeVelocity)||(velocityY >= self.swipeVelocity)) {//轻扫
  1432. if (self.scrollerView.contentOffset.y>0) {
  1433. return;
  1434. }
  1435. //可轻扫移除的方向
  1436. BOOL isX_P = NO;
  1437. BOOL isX_N = NO;
  1438. BOOL isY_P = NO;
  1439. BOOL isY_N = NO;
  1440. if ((self.dragStyle & YMDragStyleX_Positive) && velocity.x>0 && velocityX >= self.swipeVelocity) {
  1441. isX_P = self.sweepStyle & YMSweepStyleX_Positive? YES:NO;
  1442. }
  1443. if ((self.dragStyle & YMDragStyleX_Negative) && velocity.x<0 && velocityX >= self.swipeVelocity) {
  1444. isX_N = self.sweepStyle & YMSweepStyleX_Negative? YES:NO;
  1445. }
  1446. if ((self.dragStyle & YMDragStyleY_Positive) && velocity.y>0 && velocityY >= self.swipeVelocity) {
  1447. isY_P = self.sweepStyle & YMSweepStyleY_Positive? YES:NO;
  1448. }
  1449. if ((self.dragStyle & YMDragStyleY_Negative) && velocity.y <0 && velocityY >= self.swipeVelocity) {
  1450. isY_N = self.sweepStyle & YMSweepStyleY_Negative? YES:NO;
  1451. }
  1452. sweepBlock(isX_P,isX_N,isY_P,isY_N);
  1453. // LSTPVLog(@"isX=%@,isY=%@,velocityX=%lf,velocityY=%lf",isX?@"YES":@"NO",isY?@"YES":@"NO",velocityX,velocityY);
  1454. }else {//普通拖拽
  1455. BOOL isCanDismiss = NO;
  1456. if (self.dragStyle & YMDragStyleAll) {
  1457. if (fabs(self.customView.frame.origin.x - self.originFrame.origin.x)>=self.dragDistance && self.dragDistance!=0) {
  1458. isCanDismiss = YES;
  1459. }
  1460. if (fabs(self.customView.frame.origin.y - self.originFrame.origin.y)>=self.dragDistance && self.dragDistance!=0) {
  1461. isCanDismiss = YES;
  1462. }
  1463. if (isCanDismiss) {
  1464. [self dismissWithStyle:_isDragDismissStyle? self.dragDismissStyle:self.dismissStyle
  1465. duration:self.getDragDismissDuration];
  1466. }else {
  1467. dragReboundBlock();
  1468. }
  1469. } else {
  1470. dragReboundBlock();
  1471. }
  1472. }
  1473. }
  1474. }
  1475. #pragma mark - ***** 懒加载 *****
  1476. - (NSHashTable<id<YMPopupViewProtocol>> *)delegates {
  1477. if (_delegates) return _delegates;
  1478. _delegates = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
  1479. return _delegates;
  1480. }
  1481. - (UIView *)parentView {
  1482. return self.container;
  1483. }
  1484. - (UIView *)currCustomView {
  1485. return self.customView;
  1486. }
  1487. - (NSHashTable *)popViewDidDismissBlocks
  1488. {
  1489. if (_popViewDidDismissBlocks) return _popViewDidDismissBlocks;
  1490. _popViewDidDismissBlocks = [NSHashTable hashTableWithOptions:(NSPointerFunctionsWeakMemory)];
  1491. return _popViewDidDismissBlocks;
  1492. }
  1493. #pragma mark - ***** 以下是 窗口管理api *****
  1494. /** 保存popView */
  1495. + (void)savePopView:(YMPopupView *)popView {
  1496. [YMPopupViewManager savePopView:popView];
  1497. }
  1498. /** 获取全局(整个app内)所有popView */
  1499. + (NSArray *)getAllPopView {
  1500. return [YMPopupViewManager getAllPopView];
  1501. }
  1502. /** 获取当前页面所有popView */
  1503. + (NSArray *)getAllPopViewForParentView:(UIView *)parentView {
  1504. return [YMPopupViewManager getAllPopViewForParentView:parentView];
  1505. }
  1506. /** 获取当前页面指定编队的所有popView */
  1507. + (NSArray *)getAllPopViewForPopView:(YMPopupView *)popView {
  1508. return [YMPopupViewManager getAllPopViewForPopView:popView];
  1509. }
  1510. /**
  1511. 读取popView (有可能会跨编队读取弹窗)
  1512. 建议使用getPopViewForGroupId:forkey: 方法进行精确读取
  1513. */
  1514. + (YMPopupView *)getPopViewForKey:(NSString *)key {
  1515. return [YMPopupViewManager getPopViewForKey:key];
  1516. }
  1517. /** 移除popView */
  1518. + (void)removePopView:(YMPopupView *)popView {
  1519. [YMPopupViewManager removePopView:popView];
  1520. }
  1521. + (void)removePopView:(YMPopupView *)popView complete:(YmVoidBlock)complete
  1522. {
  1523. if (popView == nil) {
  1524. complete ? complete() : nil;
  1525. }
  1526. popView.popViewDidDismissBlock = complete;
  1527. [YMPopupViewManager removePopView:popView];
  1528. }
  1529. /**
  1530. 移除popView 通过唯一key (有可能会跨编队误删弹窗)
  1531. 建议使用removePopViewForGroupId:forkey: 方法进行精确删除
  1532. */
  1533. + (void)removePopViewForKey:(NSString *)key {
  1534. [YMPopupViewManager removePopViewForKey:key complete:nil];
  1535. }
  1536. + (void)removePopViewForKey:(NSString *)key complete:(YmVoidBlock)complete
  1537. {
  1538. [YMPopupViewManager removePopViewForKey:key complete:complete];
  1539. }
  1540. /** 移除所有popView */
  1541. + (void)removeAllPopView {
  1542. [YMPopupViewManager removeAllPopViewComplete:nil];
  1543. }
  1544. + (void)removeAllPopViewComplete:(YmVoidBlock)complete
  1545. {
  1546. [YMPopupViewManager removeAllPopViewComplete:complete];
  1547. }
  1548. /** 移除 最后一个弹出的 popView */
  1549. + (void)removeLastPopView {
  1550. [YMPopupViewManager removeLastPopViewComplete:nil];
  1551. }
  1552. + (void)removeLastPopViewComplete:(YmVoidBlock)complete
  1553. {
  1554. [YMPopupViewManager removeLastPopViewComplete:complete];
  1555. }
  1556. /** 开启调试view 建议设置成 线上隐藏 测试打开 */
  1557. + (void)setLogStyle:(YMPopupViewLogStyle)logStyle {
  1558. _logStyle = logStyle;
  1559. if (logStyle == YMPopupViewLogStyleNO) {
  1560. [YMPopupViewM().infoView removeFromSuperview];
  1561. YMPopupViewM().infoView = nil;
  1562. }
  1563. }
  1564. @end