YHSharePresentView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // YHSharePresentView.m
  3. // PikeWay
  4. //
  5. // Created by YHIOS002 on 16/10/27.
  6. // Copyright © 2016年 YHSoft. All rights reserved.
  7. //
  8. #import "YHSharePresentView.h"
  9. //#import "YHSocialShareManager.h"
  10. #define kPopViewBGColor RGBCOLOR(224.0f, 224.0f, 224.0f) //collectionView背景颜色
  11. #define kMaskBGColor [RGBCOLOR(0, 0, 0) colorWithAlphaComponent:0.5] //遮罩背景颜色
  12. #define kTextColor [UIColor blackColor]
  13. #define kCancelBtnColor RGBCOLOR(0.f, 191.0f, 143.f)
  14. //collectionview边距
  15. static const CGFloat kLSpace = 10.0f;//左
  16. static const CGFloat kRSpace = 10.0f;//右
  17. static const CGFloat kTSpace = 15.0f;//上
  18. static const CGFloat kBSpace = 10.0f;//下
  19. static const CGFloat kLineSpace = 15.0f;//行边距
  20. static const CGFloat kTitleH = 30.0f;
  21. static const CGFloat kCancelBtnH = 55.0f; //取消按钮高度
  22. static const CGFloat kIconW = 55.0f; //图标宽(默认宽高相等)
  23. static const CGFloat kFontSize = 10.0f; //字体大小
  24. @interface ColCellForShareItem : UICollectionViewCell
  25. @property (nonatomic,strong) UILabel *lbItemName;
  26. @property (nonatomic,strong) UIImageView *imgvIcon;
  27. @property (nonatomic,strong) NSDictionary *dictConfig;
  28. @end
  29. @implementation ColCellForShareItem
  30. - (instancetype)initWithFrame:(CGRect)frame{
  31. if (self = [super initWithFrame:frame]) {
  32. [self setup];
  33. // self.backgroundColor = [UIColor redColor];
  34. }
  35. return self;
  36. }
  37. - (void)setup{
  38. UIImageView *imgvIcon = [UIImageView new];
  39. [self addSubview:imgvIcon];
  40. _imgvIcon = imgvIcon;
  41. UILabel *lbItemName = [UILabel new];
  42. lbItemName.font = [UIFont systemFontOfSize:kFontSize];
  43. lbItemName.textColor = kTextColor;
  44. lbItemName.textAlignment = NSTextAlignmentCenter;
  45. [self addSubview:lbItemName];
  46. _lbItemName = lbItemName;
  47. [self layoutUI];
  48. //kun调试
  49. // _imgvIcon.backgroundColor = [UIColor yellowColor];
  50. // _lbItemName.backgroundColor = [UIColor greenColor];
  51. }
  52. - (void)layoutUI{
  53. __weak __typeof(self)weakSelf = self;
  54. [_imgvIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.centerX.equalTo(weakSelf.mas_centerX);
  56. make.width.height.mas_equalTo(kIconW);
  57. make.bottom.equalTo(weakSelf.lbItemName.mas_top).offset(-5);
  58. }];
  59. [_lbItemName mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.bottom.equalTo(weakSelf);
  61. make.centerX.equalTo(weakSelf.mas_centerX);
  62. make.width.equalTo(weakSelf.mas_width).multipliedBy(0.9);
  63. }];
  64. }
  65. #pragma mark - Setter
  66. - (void)setDictConfig:(NSDictionary *)dictConfig{
  67. _dictConfig = dictConfig;
  68. CGFloat fontSize = [_dictConfig[@"fontSize"] floatValue];
  69. if (fontSize) {
  70. _lbItemName.font = [UIFont systemFontOfSize:fontSize];
  71. }
  72. CGFloat cancelBtnH =[_dictConfig[@"cancelBtnH"] floatValue];
  73. if (cancelBtnH) {
  74. }
  75. UIColor *textColor =_dictConfig[@"textColor"];
  76. _lbItemName.textColor = textColor;
  77. }
  78. @end
  79. @interface YHFLayoutShareItem : UICollectionViewFlowLayout
  80. @end
  81. @implementation YHFLayoutShareItem
  82. - (void)prepareLayout{
  83. self.sectionInset = UIEdgeInsetsMake(kTSpace, kLSpace, kBSpace, kRSpace);
  84. CGFloat itemW = (kScreenW - kLSpace - kRSpace)/4;
  85. self.itemSize = CGSizeMake(itemW, itemW);
  86. self.minimumLineSpacing = kLineSpace;
  87. self.minimumInteritemSpacing = 0;
  88. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  89. }
  90. @end
  91. @interface YHSharePresentView()<UICollectionViewDelegate,UICollectionViewDataSource>
  92. @property (nonatomic,strong) UIView *viewTitle;
  93. @property (nonatomic,strong) UILabel *lbTitle;
  94. @property (nonatomic,strong) UICollectionView *collectionView;
  95. @property (nonatomic,strong) UILabel *lbCancel;
  96. @property (nonatomic,strong) UIView *viewBG;
  97. @property (nonatomic,strong) NSMutableArray *itemHiddenArray;
  98. //标记变量
  99. @property (nonatomic,copy) DismissBlock dBlock;
  100. @property (nonatomic,assign) CGFloat popViewH;
  101. @property (nonatomic,strong) NSDictionary *dictConfig;
  102. @end
  103. @implementation YHSharePresentView
  104. - (instancetype)initWithFrame:(CGRect)frame{
  105. if (self = [super initWithFrame:frame]) {
  106. [self setup];
  107. }
  108. return self;
  109. }
  110. - (void)setup{
  111. UIView *viewBG = [UIView new];
  112. viewBG.backgroundColor = kMaskBGColor;
  113. UITapGestureRecognizer *tapViewBG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onCancel:)];
  114. [viewBG addGestureRecognizer:tapViewBG];
  115. [self addSubview:viewBG];
  116. _viewBG = viewBG;
  117. UIView *viewTitle = [UIView new];
  118. viewTitle.backgroundColor = kPopViewBGColor;
  119. [self addSubview:viewTitle];
  120. _viewTitle = viewTitle;
  121. UILabel *lbTitle = [UILabel new];
  122. lbTitle.text = ASLocalizedString(@"分享到");
  123. lbTitle.textAlignment = NSTextAlignmentLeft;
  124. lbTitle.textColor = [UIColor colorWithWhite:0.376 alpha:1.000];
  125. lbTitle.backgroundColor = kPopViewBGColor;
  126. lbTitle.font = [UIFont systemFontOfSize:14.0f];
  127. [viewTitle addSubview:lbTitle];
  128. _lbTitle = lbTitle;
  129. YHFLayoutShareItem *layout = [[YHFLayoutShareItem alloc] init];
  130. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  131. collectionView.backgroundColor = kPopViewBGColor;
  132. collectionView.delegate = self;
  133. collectionView.dataSource = self;
  134. [collectionView registerClass:[ColCellForShareItem class] forCellWithReuseIdentifier:NSStringFromClass([ColCellForShareItem class])];
  135. [self addSubview:collectionView];
  136. _collectionView = collectionView;
  137. UILabel *lbCancel = [UILabel new];
  138. lbCancel.font = [UIFont systemFontOfSize:14.0];
  139. lbCancel.textAlignment = NSTextAlignmentCenter;
  140. UITapGestureRecognizer *tapLbCancel =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onCancel:)];
  141. [lbCancel addGestureRecognizer:tapLbCancel];
  142. lbCancel.text = ASLocalizedString(@"取消");
  143. lbCancel.textColor = [UIColor whiteColor];
  144. lbCancel.backgroundColor = kCancelBtnColor;
  145. [self addSubview:lbCancel];
  146. _lbCancel = lbCancel;
  147. }
  148. - (void)layoutUI{
  149. CGFloat itemCount = self.itemNameArray.count;
  150. NSParameterAssert(itemCount);
  151. //行数
  152. CGFloat rows = ceilf(itemCount / 4);
  153. //item垂直方向的高度
  154. CGFloat itemVH = (kScreenW - kLSpace - kRSpace)/4;
  155. //item所有行的行高
  156. CGFloat collectionViewH = (itemVH *rows + (rows-1)*kLineSpace + kTSpace + kBSpace);
  157. //取消按钮高度
  158. _cancelBtnH = _cancelBtnH? _cancelBtnH:kCancelBtnH;
  159. //弹出视图高度
  160. _popViewH = collectionViewH+kCancelBtnH+kTitleH;
  161. // self.lbTitle.backgroundColor = [UIColor greenColor];
  162. __weak __typeof(self)weakSelf = self;
  163. [self.viewTitle mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.left.right.equalTo(weakSelf);
  165. make.height.mas_equalTo(kTitleH);
  166. }];
  167. [self.lbTitle mas_makeConstraints:^(MASConstraintMaker *make) {
  168. make.left.equalTo(weakSelf.viewTitle).offset(15);
  169. make.bottom.right.equalTo(weakSelf.viewTitle);
  170. }];
  171. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  172. make.top.equalTo(weakSelf.viewTitle.mas_bottom);
  173. make.left.right.equalTo(weakSelf);
  174. make.bottom.equalTo(weakSelf.lbCancel.mas_top);
  175. make.height.mas_equalTo(collectionViewH);
  176. }];
  177. [self.lbCancel mas_makeConstraints:^(MASConstraintMaker *make) {
  178. make.left.right.equalTo(weakSelf);
  179. make.bottom.equalTo(weakSelf).offset(weakSelf.popViewH);
  180. make.height.mas_equalTo(weakSelf.cancelBtnH);
  181. }];
  182. [self.viewBG mas_makeConstraints:^(MASConstraintMaker *make) {
  183. make.edges.equalTo(weakSelf);
  184. }];
  185. }
  186. #pragma mark - Lazy Load
  187. - (NSDictionary *)dictConfig{
  188. if (!_dictConfig) {
  189. _textColor = _textColor?_textColor:kTextColor;
  190. _maskColor = _maskColor?_maskColor:kMaskBGColor;
  191. _popViewBGColor = _popViewBGColor?_popViewBGColor:kPopViewBGColor;
  192. _cancelBtnColor = _cancelBtnColor?_cancelBtnColor:kCancelBtnColor;
  193. _dictConfig = @{
  194. @"fontSize":@(_fontSize),
  195. @"textColor":_textColor,
  196. @"maskColor":_maskColor,
  197. @"popViewBGColor":_popViewBGColor,
  198. @"cancelBtnColor":_cancelBtnColor,
  199. };
  200. __weak __typeof(self)weakSelf = self;
  201. if (_cancelBtnH) {
  202. [self.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
  203. make.height.mas_equalTo(weakSelf.cancelBtnH);
  204. }];
  205. }
  206. }
  207. return _dictConfig;
  208. }
  209. - (NSMutableArray *)itemHiddenArray{
  210. if (!_itemHiddenArray) {
  211. _itemHiddenArray = [NSMutableArray new];
  212. }
  213. return _itemHiddenArray;
  214. }
  215. #pragma mark - Setter
  216. - (void)setCancelBtnColor:(UIColor *)cancelBtnColor{
  217. _cancelBtnColor = cancelBtnColor;
  218. if (_cancelBtnColor) {
  219. self.lbCancel.backgroundColor = _cancelBtnColor;
  220. }
  221. }
  222. - (void)setPopViewBGColor:(UIColor *)popViewBGColor{
  223. _popViewBGColor = popViewBGColor;
  224. if (_popViewBGColor) {
  225. self.collectionView.backgroundColor = _popViewBGColor;
  226. }
  227. }
  228. - (void)setMaskColor:(UIColor *)maskColor{
  229. _maskColor = maskColor;
  230. if (_maskColor) {
  231. self.viewBG.backgroundColor = _maskColor;
  232. }
  233. }
  234. - (void)setShareType:(ShareType)shareType{
  235. _shareType = shareType;
  236. switch (_shareType) {
  237. case ShareType_WorkGroup:
  238. {
  239. self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO),@(NO)]];
  240. // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
  241. // self.itemHiddenArray[0] = @(YES);
  242. // self.itemHiddenArray[1] = @(YES);
  243. // }
  244. self.iconNameArray = @[@"workgroup_sharetowechatfriendcircle",@"workgroup_sharetowechatfriend",@"workgroup_sharetopikewaydynamic"];
  245. self.itemNameArray = @[ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友"),ASLocalizedString(@"动态")];
  246. }
  247. break;
  248. case ShareType_Card:
  249. {
  250. self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO)]];
  251. // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
  252. // self.itemHiddenArray[0] = @(YES);
  253. // self.itemHiddenArray[1] = @(YES);
  254. // }
  255. self.iconNameArray = @[@"workgroup_sharetowechatfriendcircle",@"workgroup_sharetowechatfriend"];
  256. self.itemNameArray = @[ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友")];
  257. }
  258. break;
  259. case ShareType_News:
  260. {
  261. self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO),@(NO)]];
  262. // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
  263. // self.itemHiddenArray[1] = @(YES);
  264. // self.itemHiddenArray[2] = @(YES);
  265. // }
  266. // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_QQ]) {
  267. // self.itemHiddenArray[0] = @(YES);
  268. // }
  269. self.iconNameArray = @[@"common_img_qq",@"common_img_timeline",@"common_img_wechatFri"];
  270. self.itemNameArray = @[@"qq",ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友")];
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. - (void)setItemNameArray:(NSArray *)itemNameArray{
  278. _itemNameArray = itemNameArray;
  279. [self layoutUI];
  280. }
  281. #pragma mark - UICollectionViewDataSource
  282. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  283. return self.itemNameArray.count;
  284. }
  285. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  286. ColCellForShareItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ColCellForShareItem class]) forIndexPath:indexPath];
  287. if (indexPath.row < self.iconNameArray.count) {
  288. cell.imgvIcon.image = [UIImage imageNamed:self.iconNameArray[indexPath.row]];
  289. }
  290. if (indexPath.row < self.itemNameArray.count) {
  291. cell.lbItemName.text = self.itemNameArray[indexPath.row];
  292. }
  293. if (indexPath.row < self.itemHiddenArray.count) {
  294. BOOL hidden = [self.itemHiddenArray[indexPath.row] boolValue];
  295. cell.hidden = hidden;
  296. }
  297. cell.dictConfig = self.dictConfig;
  298. return cell;
  299. }
  300. #pragma mark - UICollectionViewDelegate
  301. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  302. if (_dBlock) {
  303. _dBlock(NO,indexPath.row);
  304. }
  305. [self hide];
  306. }
  307. #pragma mark - Action
  308. - (void)onCancel:(id)sender{
  309. [self hide];
  310. if (_dBlock) {
  311. _dBlock(YES,-1);
  312. }
  313. }
  314. #pragma mark - Public
  315. - (void)show{
  316. self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
  317. [self.window addSubview:self];
  318. //视图弹出
  319. __weak __typeof(self)weakSelf = self;
  320. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  321. [weakSelf.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
  322. make.bottom.equalTo(weakSelf);
  323. }];
  324. [UIView animateWithDuration:0.25 animations:^{
  325. [weakSelf layoutIfNeeded];
  326. }];
  327. });
  328. }
  329. - (void)hide{
  330. __weak __typeof(self)weakSelf = self;
  331. [weakSelf.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
  332. make.bottom.equalTo(weakSelf).offset(weakSelf.popViewH);
  333. }];
  334. [UIView animateWithDuration:0.25 animations:^{
  335. [self layoutIfNeeded];
  336. }completion:^(BOOL finished) {
  337. [self removeFromSuperview];
  338. }];
  339. }
  340. - (void)dismissHandler:(DismissBlock)handler{
  341. _dBlock = handler;
  342. }
  343. /*
  344. // Only override drawRect: if you perform custom drawing.
  345. // An empty implementation adversely affects performance during animation.
  346. - (void)drawRect:(CGRect)rect {
  347. // Drawing code
  348. }
  349. */
  350. @end