Keine Beschreibung

FKShareView.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // FKShareView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/12/30.
  6. // Copyright © 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKShareView.h"
  9. #import "FKRedPacketRequest.h"
  10. #import "FKRedPacketReform.h"
  11. #import "FKTargetConfigUtil.h"
  12. #define kShareActionBtnTagBase 900
  13. #define kShareContentHeight 310
  14. #define kShareAnimationDuration 0.2f
  15. int const FKRewardRequestDetail = 1001;
  16. @interface FKShareView ()
  17. <FLNetworkDelegate>
  18. @property (nonatomic, strong) UIImageView *headerImgView;
  19. @property (nonatomic, strong) UILabel *rewardLabel;
  20. @property (nonatomic, strong) UILabel *titleLabel;
  21. @property (nonatomic, strong) UIView *contentView;
  22. @property (nonatomic, strong) UIView *bgView;
  23. @property (nonatomic, strong) UIView *actionView;
  24. @property (nonatomic, strong) UIView *middleLine;
  25. @property (nonatomic, strong) UIButton *shareQuestionBtn;
  26. @property (nonatomic, copy) void (^acitonBlock)(kShareViewActionType type, FKShareView *shareView);
  27. @end
  28. @implementation FKShareView
  29. - (instancetype)initWithLogin:(BOOL)islogin {
  30. self = [super init];
  31. if (islogin) {
  32. [self layoutWithLogin];
  33. [self addNotify];
  34. } else {
  35. [self layoutNotLogin];
  36. [self addNotify];
  37. }
  38. return self;
  39. }
  40. - (instancetype)init{
  41. if (self = [super init]) {
  42. [self layoutWithLogin];
  43. [self addNotify];
  44. }
  45. return self;
  46. }
  47. #pragma mark - Response
  48. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  49. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  50. if (identify == FKRewardRequestDetail){
  51. NSString *rewardText = [FKRedPacketReform parserAwardTipWithDict:response];
  52. self.rewardLabel.text = rewardText;
  53. }
  54. }
  55. }
  56. #pragma mark - Layout
  57. - (void)layoutWithLogin{
  58. [self addSubview:self.bgView];
  59. [self addSubview:self.contentView];
  60. [self.contentView addSubview:self.headerImgView];
  61. [self.contentView addSubview:self.rewardLabel];
  62. [self.contentView addSubview:self.titleLabel];
  63. [self.contentView addSubview:self.actionView];
  64. [self.contentView addSubview:self.middleLine];
  65. [self.contentView addSubview:self.shareQuestionBtn];
  66. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.edges.insets(UIEdgeInsetsZero);
  68. }];
  69. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.left.right.bottom.equalTo(self);
  71. make.height.mas_equalTo(IS_IPHONE_X ? 272 : 248);
  72. }];
  73. [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.bottom.equalTo(self.contentView.mas_top).offset(41);
  75. make.centerX.equalTo(self.contentView);
  76. }];
  77. [self.rewardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.top.equalTo(self.headerImgView.mas_bottom).offset(9);
  79. make.centerX.equalTo(self.contentView);
  80. }];
  81. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.top.equalTo(self.headerImgView.mas_bottom).offset(30);
  83. make.centerX.equalTo(self.contentView);
  84. make.width.mas_equalTo(270);
  85. }];
  86. [self.shareQuestionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.size.mas_equalTo(CGSizeMake(47, 53));
  88. make.top.right.equalTo(self.contentView);
  89. }];
  90. [self.actionView mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.right.bottom.equalTo(self.contentView);
  92. make.height.mas_equalTo(IS_IPHONE_X ? 149 : 125);
  93. }];
  94. [self.middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.left.right.top.equalTo(self.actionView);
  96. make.height.mas_equalTo(0.5);
  97. }];
  98. [self layoutActionView];
  99. }
  100. - (void)layoutNotLogin {
  101. [self addSubview:self.bgView];
  102. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.edges.insets(UIEdgeInsetsZero);
  104. }];
  105. [self addSubview:self.contentView];
  106. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.left.right.bottom.equalTo(self);
  108. make.height.mas_equalTo(IS_IPHONE_X ? 149 : 125);
  109. }];
  110. [self.contentView addSubview:self.actionView];
  111. [self.actionView mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.bottom.equalTo(self.contentView);
  113. make.left.equalTo(self.contentView).offset(8);
  114. make.right.equalTo(self.contentView).offset(-8);
  115. make.height.mas_equalTo(IS_IPHONE_X ? 149 : 125);
  116. }];
  117. [self layoutActionView];
  118. }
  119. - (void)layoutActionView{
  120. UIButton *wxFriendBtn = [self commonActionBtnWithTag:1 + kShareActionBtnTagBase];
  121. UIButton *wxCircelBtn = [self commonActionBtnWithTag:2 + kShareActionBtnTagBase];
  122. UIButton *weiBoBtn = [self commonActionBtnWithTag:3 + kShareActionBtnTagBase];
  123. UIButton *qqZoneBtn = [self commonActionBtnWithTag:4 + kShareActionBtnTagBase];
  124. UIButton *copyURLBtn = [self commonActionBtnWithTag:5 + kShareActionBtnTagBase];
  125. [self.actionView addSubview:wxFriendBtn];
  126. [self.actionView addSubview:wxCircelBtn];
  127. [self.actionView addSubview:weiBoBtn];
  128. [self.actionView addSubview:qqZoneBtn];
  129. [self.actionView addSubview:copyURLBtn];
  130. [wxFriendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.top.left.bottom.equalTo(self.actionView);
  132. make.width.mas_equalTo(weiBoBtn);
  133. }];
  134. [wxCircelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.top.bottom.equalTo(self.contentView);
  136. make.left.equalTo(wxFriendBtn.mas_right);
  137. make.width.mas_equalTo(wxFriendBtn);
  138. }];
  139. [weiBoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.top.bottom.equalTo(self.contentView);
  141. make.left.equalTo(wxCircelBtn.mas_right);
  142. make.width.mas_equalTo(wxCircelBtn);
  143. }];
  144. [qqZoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  145. make.top.bottom.equalTo(self.contentView);
  146. make.left.equalTo(weiBoBtn.mas_right);
  147. make.width.mas_equalTo(weiBoBtn);
  148. }];
  149. [copyURLBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.top.bottom.equalTo(self.contentView);
  151. make.left.equalTo(qqZoneBtn.mas_right);
  152. make.right.equalTo(self.actionView);
  153. make.width.mas_equalTo(qqZoneBtn);
  154. }];
  155. [self layoutActionAreaWithBtn:wxFriendBtn img:@"share_weixin_friend" title:@"微信好友"];
  156. [self layoutActionAreaWithBtn:wxCircelBtn img:@"share_weixin_circle" title:@"朋友圈"];
  157. [self layoutActionAreaWithBtn:weiBoBtn img:@"share_to_weibo" title:@"新浪微博"];
  158. [self layoutActionAreaWithBtn:qqZoneBtn img:@"share_to_qqZone" title:@"QQ空间"];
  159. [self layoutActionAreaWithBtn:copyURLBtn img:@"share_url_copy_icon" title:@"复制链接"];
  160. }
  161. - (void)dealloc{
  162. [[NSNotificationCenter defaultCenter] removeObserver:self];
  163. }
  164. - (void)addNotify{
  165. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareSuccess) name:FK_SHARE_SUCCESS object:nil];
  166. }
  167. - (void)shareSuccess{
  168. [self dismiss];
  169. }
  170. - (void)clickBgView{
  171. [self dismiss];
  172. }
  173. - (void)clickShareBtn:(UIButton *)sender{
  174. kShareViewActionType type = kShareViewActionTypeNone;
  175. switch (sender.tag) {
  176. case kShareActionBtnTagBase + 0:
  177. type = kShareViewActionTypeQuestion;
  178. break;
  179. case kShareActionBtnTagBase + 1:
  180. type = kShareViewActionTypeFriend;
  181. break;
  182. case kShareActionBtnTagBase + 2:
  183. type = kShareViewActionTypeCircle;
  184. break;
  185. case kShareActionBtnTagBase + 3:
  186. type = kShareViewActionTypeWeibo;
  187. break;
  188. case kShareActionBtnTagBase + 4:
  189. type = kShareViewActionTypeQQzone;
  190. break;
  191. case kShareActionBtnTagBase + 5:
  192. type = kShareViewActionTypeURLCopy;
  193. break;
  194. default:
  195. break;
  196. }
  197. if (type == kShareViewActionTypeNone) return;
  198. WeakSelf(weakSelf);
  199. if (self.acitonBlock) self.acitonBlock(type, weakSelf);
  200. }
  201. - (void)showInView:(UIView *)view actionBlock:(void (^)(kShareViewActionType type, FKShareView *shareView))action{
  202. [FKRedPacketRequest requestAwardNumber:FKRewardRequestDetail deleagate:self];
  203. if (!view) return;
  204. [self removeFromSuperview];
  205. self.acitonBlock = action;
  206. self.bgView.alpha = 0;
  207. self.contentView.transform = CGAffineTransformMakeTranslation(0, kShareContentHeight);
  208. self.frame = view.bounds;
  209. [view addSubview:self];
  210. WeakSelf(weakSelf);
  211. [UIView animateWithDuration:kShareAnimationDuration animations:^{
  212. weakSelf.bgView.alpha = 1.0f;
  213. weakSelf.contentView.transform = CGAffineTransformIdentity;
  214. } completion:nil];
  215. }
  216. - (void)dismiss{
  217. self.bgView.alpha = 1.0f;
  218. self.contentView.transform = CGAffineTransformIdentity;
  219. WeakSelf(weakSelf);
  220. [UIView animateWithDuration:kShareAnimationDuration animations:^{
  221. weakSelf.bgView.alpha = 0;
  222. weakSelf.contentView.frame = CGRectOffset(weakSelf.contentView.frame, 0, kShareContentHeight);
  223. } completion:^(BOOL finished) {
  224. if (weakSelf.acitonBlock){
  225. weakSelf.acitonBlock(kShareViewActionTypeDismiss, weakSelf);
  226. }
  227. [weakSelf removeFromSuperview];
  228. }];
  229. }
  230. - (void)dismissWithCompletion:(void(^_Nullable)(BOOL finish))completion{
  231. self.bgView.alpha = 1.0f;
  232. self.contentView.transform = CGAffineTransformIdentity;
  233. WeakSelf(weakSelf);
  234. [UIView animateWithDuration:kShareAnimationDuration animations:^{
  235. weakSelf.bgView.alpha = 0;
  236. weakSelf.contentView.frame = CGRectOffset(weakSelf.contentView.frame, 0, kShareContentHeight);
  237. } completion:^(BOOL finished) {
  238. if (completion){
  239. completion(finished);
  240. }
  241. [weakSelf removeFromSuperview];
  242. }];
  243. }
  244. - (UIButton *)commonActionBtnWithTag:(NSInteger)tag{
  245. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  246. [button addTarget:self action:@selector(clickShareBtn:) forControlEvents:UIControlEventTouchUpInside];
  247. button.tag = tag;
  248. return button;
  249. }
  250. - (void)layoutActionAreaWithBtn:(UIButton *)actionBtn
  251. img:(NSString *)img
  252. title:(NSString *)title{
  253. UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:img]];
  254. UILabel *titleLabel = ({
  255. UILabel *label = [[UILabel alloc]init];
  256. label.font = [UIFont systemFontOfSize:13];
  257. label.textColor = UIColorFromRGB(0x999999);
  258. label.text = title;
  259. label;
  260. });
  261. [self.actionView insertSubview:imageView belowSubview:actionBtn];
  262. [self.actionView insertSubview:titleLabel belowSubview:actionBtn];
  263. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  264. make.top.equalTo(self.actionView).offset(25);
  265. make.centerX.equalTo(actionBtn);
  266. }];
  267. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  268. make.top.equalTo(imageView.mas_bottom).offset(10);
  269. make.centerX.equalTo(imageView);
  270. }];
  271. }
  272. #pragma mark - property
  273. - (UIImageView *)headerImgView{
  274. if (_headerImgView == nil) {
  275. _headerImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shareHeader"]];
  276. }
  277. return _headerImgView;
  278. }
  279. - (UILabel *)rewardLabel{
  280. if (_rewardLabel == nil) {
  281. _rewardLabel = [[UILabel alloc]init];
  282. _rewardLabel.textColor = UIColorFromRGB(0xff6362);
  283. _rewardLabel.font = [UIFont systemFontOfSize:13];
  284. _rewardLabel.textAlignment = NSTextAlignmentCenter;
  285. _rewardLabel.numberOfLines = 0;
  286. }
  287. return _rewardLabel;
  288. }
  289. - (UILabel *)titleLabel{
  290. if (_titleLabel == nil) {
  291. _titleLabel = [[UILabel alloc]init];
  292. _titleLabel.font = [UIFont systemFontOfSize:14];
  293. _titleLabel.textColor = UIColorFromRGB(0x666666);
  294. _titleLabel.numberOfLines = 0;
  295. _titleLabel.textAlignment = NSTextAlignmentCenter;
  296. _titleLabel.text = [NSString stringWithFormat:@"你的小伙伴通过你分享的链接进到%@你就可以获得相应的积分奖励哦", [FKTargetConfigUtil appName]];
  297. }
  298. return _titleLabel;
  299. }
  300. - (UIView *)contentView{
  301. if (_contentView == nil) {
  302. _contentView = [[UIView alloc]init];
  303. _contentView.backgroundColor = [UIColor whiteColor];
  304. }
  305. return _contentView;
  306. }
  307. - (UIView *)bgView{
  308. if (_bgView == nil) {
  309. _bgView = [[ UIView alloc]init];
  310. _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
  311. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBgView)];
  312. tap.numberOfTapsRequired = 1;
  313. tap.numberOfTouchesRequired = 1;
  314. [_bgView addGestureRecognizer:tap];
  315. }
  316. return _bgView;
  317. }
  318. - (UIView *)actionView{
  319. if (_actionView == nil) {
  320. _actionView = [[UIView alloc]init];
  321. _actionView.backgroundColor = [UIColor whiteColor];
  322. }
  323. return _actionView;
  324. }
  325. - (UIView *)middleLine{
  326. if (_middleLine == nil){
  327. _middleLine =[[UIView alloc]init];
  328. _middleLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
  329. }
  330. return _middleLine;
  331. }
  332. - (UIButton *)shareQuestionBtn{
  333. if (_shareQuestionBtn == nil) {
  334. _shareQuestionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  335. _shareQuestionBtn.tag = kShareActionBtnTagBase + 0;
  336. [_shareQuestionBtn setImage:[UIImage imageNamed:@"shareQuestion"] forState:UIControlStateNormal];
  337. [_shareQuestionBtn addTarget:self action:@selector(clickShareBtn:) forControlEvents:UIControlEventTouchUpInside];
  338. }
  339. return _shareQuestionBtn;
  340. }
  341. @end