PopView.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. //
  2. // PopView.m
  3. // iPad-Education
  4. //
  5. // Created by 李林 on 2017/10/18.
  6. // Copyright © 2017年 iLaihua. All rights reserved.
  7. //
  8. #import "PopView.h"
  9. #import "PopAnimationTool.h"
  10. @interface PopView()<CAAnimationDelegate,UIGestureRecognizerDelegate>
  11. @property (nonatomic ,weak) UIView *contentView;
  12. @property (nonatomic ,weak) UIView *onView;
  13. @property (nonatomic ,assign) PopViewDirection direct;
  14. @property (nonatomic ,assign) BOOL animation;
  15. @property (nonatomic ,strong) UIView *triangleView;
  16. @property (nonatomic ,assign) CGFloat offset;
  17. @property (nonatomic ,strong) UIResponder *backCtl;
  18. @property (nonatomic ,strong) CABasicAnimation *showAnimation;
  19. @property (nonatomic ,strong) CABasicAnimation *hidenAnimation;
  20. @end
  21. @implementation PopView
  22. static NSInteger const popViewTag = 364;
  23. + (instancetype)getCurrentPopView{
  24. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  25. PopView *oldPopView = (PopView *)[window viewWithTag:popViewTag];
  26. return oldPopView;
  27. }
  28. #pragma mark- popAnimation
  29. + (instancetype)popUpContentView:(UIView *)contentView
  30. direct:(PopViewDirection)direct
  31. onView:(UIView *)onView{
  32. return [self popUpContentView:contentView direct:direct onView:onView offset:0 triangleView:nil animation:YES];
  33. }
  34. + (instancetype)popUpContentView:(UIView *)contentView
  35. direct:(PopViewDirection)direct
  36. onView:(UIView *)onView
  37. offset:(CGFloat)offset
  38. triangleView:(UIView *)triangleView
  39. animation:(BOOL)animation{
  40. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  41. PopView *oldPopView = [self getCurrentPopView];
  42. PopView *newPopView = [[PopView alloc] initWithFrame:window.bounds
  43. direct:direct
  44. onView:onView
  45. contentView:contentView
  46. offSet:offset
  47. triangleView:triangleView
  48. animation:animation];
  49. [window addSubview:newPopView];
  50. newPopView.showAnimation = [PopAnimationTool getShowPopAnimationWithType:direct contentView:contentView belowView:nil];
  51. newPopView.hidenAnimation = [PopAnimationTool getHidenPopAnimationWithType:direct contentView:contentView belowView:nil];
  52. [newPopView setPopMenuSubViewFrame];
  53. [newPopView animationPopContainerViewWithOldPopView:oldPopView];
  54. [newPopView bringSubviewToFront:newPopView.popContainerView];
  55. contentView.layer.cornerRadius = 3;
  56. contentView.clipsToBounds = YES;
  57. return newPopView;
  58. }
  59. #pragma mark- slideAnimation
  60. + (instancetype)popSideContentView:(UIView *)contentView
  61. direct:(PopViewDirection)direction{
  62. CABasicAnimation *showAnimation = [PopAnimationTool getShowPopAnimationWithType:direction contentView:contentView belowView:nil];
  63. CABasicAnimation *hidenAnimation = [PopAnimationTool getHidenPopAnimationWithType:direction contentView:contentView belowView:nil];
  64. PopView *popView = [self popContentView:contentView showAnimation:showAnimation hidenAnimation:hidenAnimation];
  65. popView.popContainerView.center = [showAnimation.toValue CGPointValue];
  66. return popView;
  67. }
  68. + (instancetype)popSideContentView:(UIView *)contentView
  69. belowView:(UIView *)belowView;{
  70. PopViewDirection direction = PopViewDirection_SlideBelowView;
  71. CABasicAnimation *showAnimation = [PopAnimationTool getShowPopAnimationWithType:direction contentView:contentView belowView:belowView];
  72. CABasicAnimation *hidenAnimation = [PopAnimationTool getHidenPopAnimationWithType:direction contentView:contentView belowView:belowView];
  73. CGRect frame = belowView.superview.bounds;
  74. frame.origin.y = CGRectGetMaxY(belowView.frame);
  75. frame.size.height -= CGRectGetMaxY(belowView.frame);
  76. PopView *oldPopView = (PopView *)[belowView.superview viewWithTag:popViewTag];
  77. PopView *newPopView = [[PopView alloc] initWithFrame:frame
  78. direct:direction
  79. onView:nil
  80. contentView:contentView
  81. offSet:0
  82. triangleView:nil
  83. animation:YES];
  84. [belowView.superview insertSubview:newPopView belowSubview:belowView];
  85. newPopView.popContainerView.frame = contentView.frame;
  86. [newPopView.popContainerView addSubview:contentView];
  87. newPopView.clipsToBounds = YES;
  88. newPopView.showAnimation = showAnimation;
  89. newPopView.hidenAnimation = hidenAnimation;
  90. [newPopView animationPopContainerViewWithOldPopView:oldPopView];
  91. newPopView.popContainerView.center = [showAnimation.toValue CGPointValue];
  92. return newPopView;
  93. }
  94. + (instancetype)popContentView:(UIView *)contentView
  95. showAnimation:(CABasicAnimation *)showAnimation
  96. hidenAnimation:(CABasicAnimation *)hidenAnimation{
  97. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  98. PopView *oldPopView = (PopView *)[window viewWithTag:popViewTag];
  99. PopView *newPopView = [[PopView alloc] initWithFrame:window.bounds
  100. direct:PopViewDirection_PopUpNone
  101. onView:nil
  102. contentView:contentView
  103. offSet:0
  104. triangleView:nil
  105. animation:YES];
  106. [window addSubview:newPopView];
  107. newPopView.popContainerView.frame = contentView.frame;
  108. [newPopView.popContainerView addSubview:contentView];
  109. contentView.frame = newPopView.popContainerView.bounds;
  110. newPopView.showAnimation = showAnimation;
  111. newPopView.hidenAnimation = hidenAnimation;
  112. [newPopView animationPopContainerViewWithOldPopView:oldPopView];
  113. [newPopView bringSubviewToFront:newPopView.popContainerView];
  114. return newPopView;
  115. }
  116. - (instancetype)initWithFrame:(CGRect)frame
  117. direct:(PopViewDirection)direct
  118. onView:(UIView *)onView
  119. contentView:(UIView *)contentView
  120. offSet:(CGFloat)offset
  121. triangleView:(UIView *)triangleView
  122. animation:(BOOL)animation{
  123. self = [super initWithFrame:frame];
  124. if (self) {
  125. self.tag = popViewTag;
  126. self.direct = direct;
  127. self.contentView = contentView;
  128. self.clickOutHidden = YES;
  129. self.onView = onView;
  130. self.offset = offset;
  131. self.triangleView = triangleView ? triangleView : self.defaultTriangleView;
  132. self.animation = animation;
  133. self.keyBoardMargin = 10;
  134. UIControl *backCtl = [[UIControl alloc] initWithFrame:self.bounds];
  135. [self addSubview:backCtl];
  136. self.backCtl = backCtl;
  137. [backCtl addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  138. [self addKeyboardNotification];
  139. }
  140. return self;
  141. }
  142. - (void)setPopMenuSubViewFrame{
  143. CGRect triangleFrame = self.triangleView.bounds;
  144. CGRect contentFrame = self.contentView.bounds;
  145. CGRect popContentFrame = CGRectZero;
  146. CGRect onViewFrame = [self.onView convertRect:self.onView.bounds toView:nil];
  147. CGPoint anchorPoint = CGPointMake(.5, .5);
  148. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  149. switch (self.direct) {
  150. case PopViewDirection_PopUpBottom:
  151. //1、计算在window上的位置
  152. //1.2、计算指示器在window的位置
  153. triangleFrame.origin.y = CGRectGetMaxY(onViewFrame);
  154. triangleFrame.origin.x = onViewFrame.origin.x + onViewFrame.size.width/2 - triangleFrame.size.width/2;
  155. //1.2、计算内容在window的位置
  156. contentFrame.origin.y = CGRectGetMaxY(triangleFrame);
  157. contentFrame.origin.x = onViewFrame.origin.x + onViewFrame.size.width/2 - contentFrame.size.width/2 + self.offset;
  158. if (contentFrame.origin.x<popViewInsert) {
  159. contentFrame.origin.x = popViewInsert;
  160. }
  161. if (CGRectGetMaxX(contentFrame)>window.bounds.size.width) {
  162. contentFrame.origin.x = window.bounds.size.width - popViewInsert - contentFrame.size.width;
  163. }
  164. popContentFrame = CGRectUnion(triangleFrame, contentFrame);
  165. self.popContainerView.frame = popContentFrame;
  166. //2、计算指示器实际的位置
  167. self.triangleView.frame = triangleFrame;
  168. [window addSubview:self.triangleView];
  169. self.triangleView.frame = [self.triangleView convertRect:self.triangleView.bounds toView:self.popContainerView];
  170. [self.popContainerView addSubview:self.triangleView];
  171. //3、计算内容实际的位置
  172. self.contentView.frame = contentFrame;
  173. [window addSubview:self.contentView];
  174. self.contentView.frame = [self.contentView convertRect:self.contentView.bounds toView:self.popContainerView];
  175. [self.popContainerView addSubview:self.contentView];
  176. //4、计算锚点
  177. anchorPoint.y = 0;
  178. anchorPoint.x = (self.triangleView.frame.origin.x + self.triangleView.frame.size.width/2)/popContentFrame.size.width;
  179. self.popContainerView.layer.anchorPoint = anchorPoint;
  180. self.popContainerView.layer.position = CGPointMake(onViewFrame.origin.x + onViewFrame.size.width/2, CGRectGetMaxY(onViewFrame));
  181. break;
  182. case PopViewDirection_PopUpTop:
  183. //1、计算在window上的位置
  184. //1.2、计算指示器在window的位置
  185. triangleFrame.origin.y = onViewFrame.origin.y;
  186. triangleFrame.origin.x = onViewFrame.origin.x + onViewFrame.size.width/2 - triangleFrame.size.width/2;
  187. //1.2、计算内容在window的位置
  188. contentFrame.origin.y = triangleFrame.origin.y-10 - contentFrame.size.height;
  189. contentFrame.origin.x = onViewFrame.origin.x + onViewFrame.size.width/2 - contentFrame.size.width/2 + self.offset;
  190. if (contentFrame.origin.x<popViewInsert) {
  191. contentFrame.origin.x = popViewInsert;
  192. }
  193. if (CGRectGetMaxX(contentFrame)>window.bounds.size.width) {
  194. contentFrame.origin.x = window.bounds.size.width - popViewInsert - contentFrame.size.width;
  195. }
  196. popContentFrame = CGRectUnion(triangleFrame, contentFrame);
  197. self.popContainerView.frame = popContentFrame;
  198. //2、计算指示器实际的位置
  199. self.triangleView.frame = triangleFrame;
  200. [window addSubview:self.triangleView];
  201. self.triangleView.frame = [self.triangleView convertRect:self.triangleView.bounds toView:self.popContainerView];
  202. [self.popContainerView addSubview:self.triangleView];
  203. //3、计算内容实际的位置
  204. self.contentView.frame = contentFrame;
  205. [window addSubview:self.contentView];
  206. self.contentView.frame = [self.contentView convertRect:self.contentView.bounds toView:self.popContainerView];
  207. [self.popContainerView addSubview:self.contentView];
  208. //4、计算锚点
  209. anchorPoint.y = 1;
  210. anchorPoint.x = (self.triangleView.frame.origin.x + self.triangleView.frame.size.width/2)/popContentFrame.size.width;
  211. self.popContainerView.layer.anchorPoint = anchorPoint;
  212. self.popContainerView.layer.position = CGPointMake(onViewFrame.origin.x + onViewFrame.size.width/2, onViewFrame.origin.y);
  213. break;
  214. case PopViewDirection_PopUpRight:
  215. //1、计算在window上的位置
  216. //1.2、计算指示器在window的位置
  217. triangleFrame.origin.y = onViewFrame.origin.y + onViewFrame.size.height/2- triangleFrame.size.height/2;
  218. triangleFrame.origin.x = CGRectGetMaxX(onViewFrame);
  219. //1.2、计算内容在window的位置
  220. contentFrame.origin.y = triangleFrame.origin.y + triangleFrame.size.width/2- contentFrame.size.height/2 + self.offset;
  221. contentFrame.origin.x = CGRectGetMaxX(triangleFrame);
  222. //1、3备注:适配整个屏幕
  223. if (contentFrame.origin.y<popViewInsert) {
  224. contentFrame.origin.y = popViewInsert;
  225. }
  226. if (CGRectGetMaxY(contentFrame)>window.bounds.size.height) {
  227. contentFrame.origin.y = window.bounds.size.height - popViewInsert - contentFrame.size.height;
  228. }
  229. popContentFrame = CGRectUnion(triangleFrame, contentFrame);
  230. self.popContainerView.frame = popContentFrame;
  231. //2、计算指示器实际的位置
  232. self.triangleView.frame = triangleFrame;
  233. [window addSubview:self.triangleView];
  234. self.triangleView.frame = [self.triangleView convertRect:self.triangleView.bounds toView:self.popContainerView];
  235. [self.popContainerView addSubview:self.triangleView];
  236. //3、计算内容实际的位置
  237. self.contentView.frame = contentFrame;
  238. [window addSubview:self.contentView];
  239. self.contentView.frame = [self.contentView convertRect:self.contentView.bounds toView:self.popContainerView];
  240. [self.popContainerView addSubview:self.contentView];
  241. //4、计算锚点
  242. anchorPoint.y = (self.triangleView.frame.origin.y + self.triangleView.frame.size.height/2)/popContentFrame.size.height;
  243. anchorPoint.x = 0;
  244. self.popContainerView.layer.anchorPoint = anchorPoint;
  245. self.popContainerView.layer.position = CGPointMake(CGRectGetMaxX(onViewFrame), onViewFrame.origin.y + onViewFrame.size.height/2);
  246. //5、超出屏幕外
  247. if (CGRectGetMaxX(contentFrame)>window.bounds.size.width) {
  248. CGPoint popContentPosition = self.popContainerView.layer.position;
  249. popContentPosition.x = window.bounds.size.width - popContentFrame.size.width - popViewInsert;
  250. self.popContainerView.layer.position = popContentPosition;
  251. }
  252. break;
  253. case PopViewDirection_PopUpLeft:
  254. //1、计算在window上的位置
  255. //1.2、计算指示器在window的位置
  256. triangleFrame.origin.y = onViewFrame.origin.y + onViewFrame.size.height/2- triangleFrame.size.height/2;
  257. triangleFrame.origin.x = onViewFrame.origin.x;
  258. //1.2、计算内容在window的位置
  259. contentFrame.origin.y = onViewFrame.origin.y + onViewFrame.size.height/2- contentFrame.size.height/2 + self.offset;
  260. contentFrame.origin.x = triangleFrame.origin.x - contentFrame.size.width;
  261. //1、3备注:适配整个屏幕
  262. if (contentFrame.origin.y<popViewInsert) {
  263. contentFrame.origin.y = popViewInsert;
  264. }
  265. if (CGRectGetMaxY(contentFrame)>window.bounds.size.height) {
  266. contentFrame.origin.x = window.bounds.size.height - popViewInsert - contentFrame.size.height;
  267. }
  268. popContentFrame = CGRectUnion(triangleFrame, contentFrame);
  269. self.popContainerView.frame = popContentFrame;
  270. //2、计算指示器实际的位置
  271. self.triangleView.frame = triangleFrame;
  272. [window addSubview:self.triangleView];
  273. self.triangleView.frame = [self.triangleView convertRect:self.triangleView.bounds toView:self.popContainerView];
  274. [self.popContainerView addSubview:self.triangleView];
  275. //3、计算内容实际的位置
  276. self.contentView.frame = contentFrame;
  277. [window addSubview:self.contentView];
  278. self.contentView.frame = [self.contentView convertRect:self.contentView.bounds toView:self.popContainerView];
  279. [self.popContainerView addSubview:self.contentView];
  280. //4、计算锚点
  281. anchorPoint.y = (self.triangleView.frame.origin.y + self.triangleView.frame.size.height/2)/popContentFrame.size.height;
  282. anchorPoint.x = 1;
  283. self.popContainerView.layer.anchorPoint = anchorPoint;
  284. self.popContainerView.layer.position = CGPointMake(onViewFrame.origin.x, onViewFrame.origin.y + onViewFrame.size.height/2);
  285. break;
  286. case PopViewDirection_PopUpNone:
  287. self.contentView.frame = self.contentView.bounds;
  288. self.popContainerView.frame = self.contentView.bounds;
  289. self.popContainerView.center = window.center;
  290. [self.popContainerView addSubview:self.contentView];
  291. break;
  292. default:
  293. break;
  294. }
  295. self.triangleView.hidden = YES;// 隐藏三角形指示器
  296. }
  297. - (void)backClick{
  298. if (!keyboardShow){
  299. if (self.clickOutHidden) {
  300. [PopView hidenPopView];
  301. }
  302. }
  303. [[UIApplication sharedApplication].keyWindow endEditing:YES];
  304. }
  305. //处理响应者链
  306. //- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  307. // UIView *responseView = nil;
  308. // CGRect iframe = [self.responseOnView convertRect:self.responseOnView.bounds toView:nil];
  309. // if (CGRectContainsPoint(iframe, point)) {
  310. // for (UIView *view in self.responseOnView.subviews) {
  311. // CGRect subIframe = [view convertRect:view.bounds toView:nil];
  312. // if (CGRectContainsPoint(subIframe, point)) {
  313. // [PopView hidenPopView];
  314. // responseView = view;
  315. // }
  316. // }
  317. // if (responseView == nil) {
  318. // responseView = self.responseOnView;
  319. // }
  320. // }
  321. // responseView = [super hitTest:point withEvent:event];
  322. // //不是两个输入框切换的时候
  323. // if (![responseView isKindOfClass:[UITextView class]] && ![responseView isKindOfClass:[UITextField class]] && responseView != self.backCtl) {
  324. // [[UIApplication sharedApplication].keyWindow endEditing:YES];
  325. // }
  326. // return responseView;
  327. //}
  328. #pragma mark - animation
  329. - (void)animationPopContainerViewWithOldPopView:(PopView *)oldPopView{
  330. if (oldPopView) {
  331. if (oldPopView.willRemovedFromeSuperView) {
  332. oldPopView.willRemovedFromeSuperView();
  333. }
  334. [oldPopView removeFromSuperview];
  335. }
  336. UIColor *color = self.backgroundColor;
  337. [self.popContainerView.layer addAnimation:self.showAnimation forKey:nil];
  338. [self animationBackGroundColor:[UIColor clearColor] toColor:color];
  339. }
  340. - (void)animationBackGroundColor:(UIColor*)fromeColor toColor:(UIColor *)toColor{
  341. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
  342. animation.fromValue = (id)fromeColor.CGColor;
  343. animation.toValue = (id)toColor.CGColor;
  344. animation.duration = animationDuration;
  345. animation.fillMode = kCAFillModeForwards;
  346. animation.removedOnCompletion = NO;
  347. [self.layer addAnimation:animation forKey:@"backgroundColor"];
  348. }
  349. + (void)hidenPopView{
  350. if (keyboardShow) {
  351. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
  352. }else{
  353. UIView *superView = [UIApplication sharedApplication].keyWindow;
  354. PopView *popView = (PopView *)[superView viewWithTag:popViewTag];
  355. if (popView && ![popView.popContainerView.layer animationForKey:@"hiddenAnimation"]) {
  356. if (popView.willRemovedFromeSuperView) {
  357. popView.willRemovedFromeSuperView();
  358. }
  359. if (popView.animation && popView.hidenAnimation) {
  360. popView.hidenAnimation.removedOnCompletion = NO;
  361. [popView.popContainerView.layer addAnimation:popView.hidenAnimation forKey:@"hiddenAnimation"];
  362. [popView animationBackGroundColor:popView.backgroundColor toColor:[UIColor clearColor]];
  363. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(popView.hidenAnimation.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  364. [popView removeFromSuperview];
  365. });
  366. }else{
  367. [popView removeFromSuperview];
  368. }
  369. }
  370. }
  371. }
  372. - (void)removeFromSuperview{
  373. if (self.didRemovedFromeSuperView) {
  374. self.didRemovedFromeSuperView();
  375. }
  376. [super removeFromSuperview];
  377. }
  378. #pragma mark-处理键盘
  379. -(void)addKeyboardNotification{
  380. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  381. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  382. }
  383. -(void)dealloc{
  384. [[NSNotificationCenter defaultCenter] removeObserver:self];
  385. }
  386. static BOOL keyboardShow = NO;
  387. static CGRect popViewOriginRect;
  388. -(void)keyboardWillShow:(NSNotification *)notification{
  389. if (CGRectEqualToRect(popViewOriginRect, CGRectZero)) {
  390. popViewOriginRect = self.popContainerView.frame;
  391. }
  392. keyboardShow = YES;
  393. NSDictionary *userInfo = [notification userInfo];
  394. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  395. CGFloat keyBoardEndY = value.CGRectValue.origin.y; // 得到键盘弹出后的键盘视图所在y坐标;
  396. CGFloat animationDuration = [[userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
  397. UIView *responsInputView = [self responsInputViewOnView:self.popContainerView];
  398. if (responsInputView) {
  399. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  400. CGRect inputViewFrame = [responsInputView convertRect:responsInputView.bounds toView:window];
  401. if (CGRectGetMaxY(inputViewFrame)+self.keyBoardMargin>=keyBoardEndY) {
  402. [UIView animateWithDuration:animationDuration animations:^{
  403. CGRect _frame = self.popContainerView.frame;
  404. _frame.origin.y -= CGRectGetMaxY(inputViewFrame)+self.keyBoardMargin-keyBoardEndY;
  405. self.popContainerView.frame = _frame;
  406. }completion:^(BOOL finished) {
  407. }];
  408. }
  409. }
  410. }
  411. -(void)keyboardWillHide:(NSNotification *)notification{
  412. if (!CGRectEqualToRect(popViewOriginRect, CGRectZero)) {
  413. NSDictionary *userInfo = [notification userInfo];
  414. CGFloat animationDuration = [[userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
  415. [UIView animateWithDuration:animationDuration animations:^{
  416. self.popContainerView.frame = popViewOriginRect;
  417. }];
  418. }
  419. popViewOriginRect = CGRectZero;
  420. keyboardShow = NO;
  421. }
  422. //找到输入框
  423. - (UIView *)responsInputViewOnView:(UIView *)onView{
  424. for (UIView *view in onView.subviews) {
  425. if (([view isKindOfClass:[UITextField class]] || [view isKindOfClass:[UITextView class]]) && view.isFirstResponder) {
  426. return view;
  427. }else if(view.subviews.count>0){
  428. UIView *v = [self responsInputViewOnView:view];
  429. if (v != nil) {
  430. return v;
  431. }
  432. }
  433. }
  434. return nil;
  435. }
  436. #pragma mark-lazy
  437. - (UIView *)defaultTriangleView{
  438. CGPoint point1 = CGPointZero;
  439. CGPoint point2 = CGPointZero;
  440. CGPoint point3 = CGPointZero;
  441. CGRect triangleFrame = CGRectZero;
  442. switch (self.direct) {
  443. case PopViewDirection_PopUpLeft:
  444. point1 = CGPointMake(10, 10);
  445. point2 = CGPointMake(0, 0);
  446. point3 = CGPointMake(0, 20);
  447. triangleFrame.size = CGSizeMake(10, 20);
  448. break;
  449. case PopViewDirection_PopUpRight:
  450. point1 = CGPointMake(0, 10);
  451. point2 = CGPointMake(10, 20);
  452. point3 = CGPointMake(10, 0);
  453. triangleFrame.size = CGSizeMake(10, 20);
  454. break;
  455. case PopViewDirection_PopUpBottom:
  456. point1 = CGPointMake(10, 0);
  457. point2 = CGPointMake(20, 10);
  458. point3 = CGPointMake(0, 10);
  459. triangleFrame.size = CGSizeMake(20, 10);
  460. break;
  461. case PopViewDirection_PopUpTop:
  462. point1 = CGPointMake(0, 0);
  463. point2 = CGPointMake(20, 0);
  464. point3 = CGPointMake(10, 10);
  465. triangleFrame.size = CGSizeMake(20, 10);
  466. break;
  467. default:
  468. break;
  469. }
  470. UIBezierPath *path = [UIBezierPath bezierPath];
  471. [path moveToPoint:point1];
  472. [path addLineToPoint:point2];
  473. [path addLineToPoint:point3];
  474. [path closePath];
  475. CAShapeLayer *triangleLayer = [CAShapeLayer layer];
  476. triangleLayer.fillColor = self.contentView.backgroundColor.CGColor;
  477. triangleLayer.path = path.CGPath;
  478. UIView *defaultTriangleView = [[UIView alloc] initWithFrame:triangleFrame];
  479. [defaultTriangleView.layer addSublayer:triangleLayer];
  480. return defaultTriangleView;
  481. }
  482. - (UIView *)popContainerView{
  483. if (_popContainerView == nil) {
  484. _popContainerView = [[UIView alloc] init];
  485. [self addSubview:_popContainerView];
  486. }
  487. return _popContainerView;
  488. }
  489. @end