Aucune description

FloatDownView.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // FLRiseViewHelper.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-11-18.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "FloatDownView.h"
  9. #import "FirstLinkAppDelegate.h"
  10. #import "FLControllerHelper.h"
  11. @interface FloatDownView ()
  12. @property (nonatomic, assign) BOOL isShowing;
  13. @property (nonatomic, strong) UILabel *textLabel;
  14. @property (nonatomic, strong) UIImageView *leftIconImageView;
  15. @property (nonatomic, strong) UIButton *rightCancelBtn;
  16. @property (nonatomic, strong) UITapGestureRecognizer *clickMoreGestureRecognizer;
  17. @property (nonatomic, strong) UITapGestureRecognizer *clickCloseGestureRecognizer;
  18. @property (nonatomic, weak) UIView *pView;
  19. @property (nonatomic, assign) CGRect raiseViewFrame;
  20. @property (nonatomic, copy) void (^callBack)(id object);
  21. @end
  22. @implementation FloatDownCommand
  23. @end
  24. @implementation FloatDownView
  25. + (FloatDownView *)sharedInstance {
  26. static FloatDownView *sharedRiseViewHelperInstance = nil;
  27. static dispatch_once_t once_token;
  28. dispatch_once(&once_token, ^{
  29. sharedRiseViewHelperInstance = [[self alloc] init];
  30. });
  31. return sharedRiseViewHelperInstance;
  32. }
  33. - (void)prepareFloatDownViewFrame:(CGRect)frame inView:(UIView *)pView clickCallBack:(void (^)(id))callBack {
  34. self.raiseViewFrame = frame;
  35. self.pView = pView;
  36. self.callBack = callBack;
  37. }
  38. - (void)sendCommand:(FloatDownCommand *)command {
  39. if (self.messageQueue.count) return; // 一次只显示一个
  40. if (command) {
  41. [self.messageQueue addObject:command];
  42. }
  43. FloatDownCommand *latestCommand = self.messageQueue.lastObject;
  44. if (latestCommand) {
  45. switch (latestCommand.type) {
  46. case RiseViewTypeDefault: {
  47. break;
  48. }
  49. case RiseViewTypeAutoClose: {
  50. [self layoutForAutoCloseView];
  51. [NSTimer scheduledTimerWithTimeInterval:3.0
  52. target:self
  53. selector:@selector(closeView:)
  54. userInfo:nil
  55. repeats:FALSE];
  56. break;
  57. }
  58. case RiseViewTypeClickClose: {
  59. [self layoutForAutoClickCloseView];
  60. // [self.textLabel addGestureRecognizer:self.clickCloseGestureRecognizer];
  61. break;
  62. }
  63. case RiseViewTypeClickMore: {
  64. [self.textLabel addGestureRecognizer:self.clickMoreGestureRecognizer];
  65. break;
  66. }
  67. default:
  68. break;
  69. }
  70. [self riseBannerInView:self.pView
  71. content:latestCommand.content
  72. bgcolor:[self bgcolorWithType:latestCommand.type]
  73. textColor:[self textColorWithType:latestCommand.type]
  74. type:latestCommand.type];
  75. }
  76. }
  77. - (UIColor*)bgcolorWithType:(RiseViewType)type{
  78. switch (type) {
  79. case RiseViewTypeDefault:
  80. case RiseViewTypeAutoClose:
  81. case RiseViewTypeClickClose: {
  82. return [UIColor colorWithRed:1.0
  83. green:99.0/255.0
  84. blue:99.0/255.0 alpha:0.9];
  85. break;
  86. }
  87. case RiseViewTypeClickMore: {
  88. return [UIColor yellowColor];
  89. break;
  90. }
  91. default:
  92. break;
  93. }
  94. return [UIColor blackColor];
  95. }
  96. - (UIColor*)textColorWithType:(RiseViewType)type{
  97. switch (type) {
  98. case RiseViewTypeDefault:
  99. case RiseViewTypeAutoClose:
  100. case RiseViewTypeClickClose:
  101. case RiseViewTypeClickMore: {
  102. return [UIColor whiteColor];
  103. break;
  104. }
  105. default:
  106. break;
  107. }
  108. return [UIColor whiteColor];
  109. }
  110. - (void)riseBannerInView:(UIView *)pView
  111. content:(NSString *)content
  112. bgcolor:(UIColor *)bgcolor
  113. textColor:(UIColor *)textColor
  114. type:(RiseViewType)type
  115. {
  116. self.textLabel.textColor = textColor;
  117. self.textLabel.text = content;
  118. self.backgroundColor = bgcolor;
  119. self.frame = CGRectOffset(self.raiseViewFrame, 0, - DOWN_COMMAND_HEIGHT);
  120. [pView addSubview:self];
  121. __weak FloatDownView *weakSelf = self;
  122. [UIView animateWithDuration:0.4
  123. animations:^{
  124. weakSelf.frame = weakSelf.raiseViewFrame;
  125. } completion:^(BOOL finished) {
  126. if (type == RiseViewTypeClickClose) {
  127. // [weakSelf addSubview:weakSelf.closeImageView];
  128. }
  129. }];
  130. }
  131. #pragma mark -
  132. - (void)layoutForAutoCloseView {
  133. [self clearContent];
  134. self.backgroundColor = [UIColor colorWithRed:73.0/255.0
  135. green:208/255.0
  136. blue:187.0/255.0 alpha:0.9];
  137. WeakSelf(weakSelf);
  138. [self addSubview:self.textLabel];
  139. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.top.bottom.equalTo(weakSelf).with.offset(0);
  141. make.centerX.equalTo(weakSelf).with.offset(12);
  142. make.width.mas_greaterThanOrEqualTo(10);
  143. }];
  144. [self addSubview:self.leftIconImageView];
  145. [self.leftIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.right.equalTo(weakSelf.textLabel.mas_left).with.offset(-12);
  147. make.centerY.equalTo(weakSelf);
  148. make.size.mas_equalTo(CGSizeMake(15, 15));
  149. }];
  150. }
  151. - (void)layoutForAutoClickCloseView{
  152. [self clearContent];
  153. self.backgroundColor = [UIColorFromRGB(0xff6362) colorWithAlphaComponent:0.9];
  154. [self addSubview:self.textLabel];
  155. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  156. make.edges.insets(UIEdgeInsetsZero);
  157. }];
  158. [self addSubview:self.rightCancelBtn];
  159. [self.rightCancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  160. make.right.equalTo(self);
  161. make.width.mas_equalTo(45);
  162. make.height.centerY.equalTo(self);
  163. }];
  164. }
  165. - (void)clearContent{
  166. for (UIView *subView in self.subviews) {
  167. [subView removeFromSuperview];
  168. }
  169. }
  170. #pragma mark - Generate
  171. - (NSMutableArray*)messageQueue {
  172. if (!_messageQueue) {
  173. _messageQueue = [NSMutableArray array];
  174. }
  175. return _messageQueue;
  176. }
  177. - (UILabel*)textLabel {
  178. if (!_textLabel) {
  179. _textLabel = [[UILabel alloc] init];
  180. _textLabel.textAlignment = NSTextAlignmentCenter;
  181. _textLabel.font = [UIFont systemFontOfSize:13.0];
  182. }
  183. return _textLabel;
  184. }
  185. - (UIImageView*)leftIconImageView {
  186. if (!_leftIconImageView) {
  187. _leftIconImageView = [[UIImageView alloc] init];
  188. _leftIconImageView.image = [UIImage imageNamed:@"NewContentImageIcon"];
  189. }
  190. return _leftIconImageView;
  191. }
  192. - (UIButton *)rightCancelBtn{
  193. if (_rightCancelBtn == nil) {
  194. _rightCancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  195. [_rightCancelBtn setImage:[UIImage imageNamed:@"red_cancel"] forState:UIControlStateNormal];
  196. [_rightCancelBtn addTarget:self
  197. action:@selector(clickCancelBtn:)
  198. forControlEvents:UIControlEventTouchUpInside];
  199. }
  200. return _rightCancelBtn;
  201. }
  202. - (UITapGestureRecognizer*)clickMoreGestureRecognizer {
  203. if (!_clickMoreGestureRecognizer) {
  204. _clickMoreGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
  205. action:@selector(clickMoreAction:)];
  206. _clickMoreGestureRecognizer.numberOfTapsRequired = 1;
  207. }
  208. return _clickMoreGestureRecognizer;
  209. }
  210. - (UITapGestureRecognizer*)clickCloseGestureRecognizer {
  211. if (!_clickCloseGestureRecognizer) {
  212. _clickCloseGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
  213. action:@selector(clickCloseAction:)];
  214. _clickCloseGestureRecognizer.numberOfTapsRequired = 1;
  215. }
  216. return _clickCloseGestureRecognizer;
  217. }
  218. #pragma mark - Action Method
  219. - (IBAction)closeView:(id)sender {
  220. __weak FloatDownView *weakSelf = self;
  221. [UIView animateWithDuration:0.4
  222. animations:^{
  223. weakSelf.frame = CGRectMake(0, -CGRectGetHeight(weakSelf.raiseViewFrame), [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(weakSelf.raiseViewFrame));
  224. } completion:^(BOOL finished) {
  225. [weakSelf removeFromSuperview];
  226. [weakSelf.messageQueue removeAllObjects];
  227. }];
  228. }
  229. - (IBAction)clickMoreAction:(id)sender {
  230. [self closeView:nil];
  231. if (self.callBack) {
  232. self.callBack(nil);
  233. }
  234. }
  235. - (void)clickCancelBtn:(UIButton *)sender{
  236. [self closeView:nil];
  237. }
  238. - (IBAction)clickCloseAction:(id)sender {
  239. [self closeView:nil];
  240. }
  241. @end