| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- //
- // YHSharePresentView.m
- // PikeWay
- //
- // Created by YHIOS002 on 16/10/27.
- // Copyright © 2016年 YHSoft. All rights reserved.
- //
- #import "YHSharePresentView.h"
- //#import "YHSocialShareManager.h"
- #define kPopViewBGColor RGBCOLOR(224.0f, 224.0f, 224.0f) //collectionView背景颜色
- #define kMaskBGColor [RGBCOLOR(0, 0, 0) colorWithAlphaComponent:0.5] //遮罩背景颜色
- #define kTextColor [UIColor blackColor]
- #define kCancelBtnColor RGBCOLOR(0.f, 191.0f, 143.f)
- //collectionview边距
- static const CGFloat kLSpace = 10.0f;//左
- static const CGFloat kRSpace = 10.0f;//右
- static const CGFloat kTSpace = 15.0f;//上
- static const CGFloat kBSpace = 10.0f;//下
- static const CGFloat kLineSpace = 15.0f;//行边距
- static const CGFloat kTitleH = 30.0f;
- static const CGFloat kCancelBtnH = 55.0f; //取消按钮高度
- static const CGFloat kIconW = 55.0f; //图标宽(默认宽高相等)
- static const CGFloat kFontSize = 10.0f; //字体大小
- @interface ColCellForShareItem : UICollectionViewCell
- @property (nonatomic,strong) UILabel *lbItemName;
- @property (nonatomic,strong) UIImageView *imgvIcon;
- @property (nonatomic,strong) NSDictionary *dictConfig;
- @end
- @implementation ColCellForShareItem
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- [self setup];
- // self.backgroundColor = [UIColor redColor];
- }
- return self;
- }
- - (void)setup{
- UIImageView *imgvIcon = [UIImageView new];
- [self addSubview:imgvIcon];
- _imgvIcon = imgvIcon;
-
-
- UILabel *lbItemName = [UILabel new];
- lbItemName.font = [UIFont systemFontOfSize:kFontSize];
- lbItemName.textColor = kTextColor;
- lbItemName.textAlignment = NSTextAlignmentCenter;
- [self addSubview:lbItemName];
- _lbItemName = lbItemName;
-
- [self layoutUI];
-
- //kun调试
- // _imgvIcon.backgroundColor = [UIColor yellowColor];
- // _lbItemName.backgroundColor = [UIColor greenColor];
- }
- - (void)layoutUI{
- __weak __typeof(self)weakSelf = self;
- [_imgvIcon mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(weakSelf.mas_centerX);
- make.width.height.mas_equalTo(kIconW);
- make.bottom.equalTo(weakSelf.lbItemName.mas_top).offset(-5);
- }];
-
- [_lbItemName mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(weakSelf);
- make.centerX.equalTo(weakSelf.mas_centerX);
- make.width.equalTo(weakSelf.mas_width).multipliedBy(0.9);
- }];
- }
- #pragma mark - Setter
- - (void)setDictConfig:(NSDictionary *)dictConfig{
- _dictConfig = dictConfig;
-
- CGFloat fontSize = [_dictConfig[@"fontSize"] floatValue];
- if (fontSize) {
- _lbItemName.font = [UIFont systemFontOfSize:fontSize];
- }
-
- CGFloat cancelBtnH =[_dictConfig[@"cancelBtnH"] floatValue];
- if (cancelBtnH) {
-
- }
- UIColor *textColor =_dictConfig[@"textColor"];
- _lbItemName.textColor = textColor;
-
-
-
- }
- @end
- @interface YHFLayoutShareItem : UICollectionViewFlowLayout
- @end
- @implementation YHFLayoutShareItem
- - (void)prepareLayout{
-
- self.sectionInset = UIEdgeInsetsMake(kTSpace, kLSpace, kBSpace, kRSpace);
- CGFloat itemW = (kScreenW - kLSpace - kRSpace)/4;
- self.itemSize = CGSizeMake(itemW, itemW);
- self.minimumLineSpacing = kLineSpace;
- self.minimumInteritemSpacing = 0;
- self.scrollDirection = UICollectionViewScrollDirectionVertical;
- }
- @end
- @interface YHSharePresentView()<UICollectionViewDelegate,UICollectionViewDataSource>
- @property (nonatomic,strong) UIView *viewTitle;
- @property (nonatomic,strong) UILabel *lbTitle;
- @property (nonatomic,strong) UICollectionView *collectionView;
- @property (nonatomic,strong) UILabel *lbCancel;
- @property (nonatomic,strong) UIView *viewBG;
- @property (nonatomic,strong) NSMutableArray *itemHiddenArray;
- //标记变量
- @property (nonatomic,copy) DismissBlock dBlock;
- @property (nonatomic,assign) CGFloat popViewH;
- @property (nonatomic,strong) NSDictionary *dictConfig;
- @end
- @implementation YHSharePresentView
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- [self setup];
- }
- return self;
- }
- - (void)setup{
-
-
- UIView *viewBG = [UIView new];
- viewBG.backgroundColor = kMaskBGColor;
- UITapGestureRecognizer *tapViewBG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onCancel:)];
- [viewBG addGestureRecognizer:tapViewBG];
- [self addSubview:viewBG];
- _viewBG = viewBG;
-
-
-
- UIView *viewTitle = [UIView new];
- viewTitle.backgroundColor = kPopViewBGColor;
- [self addSubview:viewTitle];
- _viewTitle = viewTitle;
-
- UILabel *lbTitle = [UILabel new];
- lbTitle.text = ASLocalizedString(@"分享到");
- lbTitle.textAlignment = NSTextAlignmentLeft;
- lbTitle.textColor = [UIColor colorWithWhite:0.376 alpha:1.000];
- lbTitle.backgroundColor = kPopViewBGColor;
- lbTitle.font = [UIFont systemFontOfSize:14.0f];
- [viewTitle addSubview:lbTitle];
- _lbTitle = lbTitle;
-
-
- YHFLayoutShareItem *layout = [[YHFLayoutShareItem alloc] init];
- UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- collectionView.backgroundColor = kPopViewBGColor;
- collectionView.delegate = self;
- collectionView.dataSource = self;
- [collectionView registerClass:[ColCellForShareItem class] forCellWithReuseIdentifier:NSStringFromClass([ColCellForShareItem class])];
- [self addSubview:collectionView];
- _collectionView = collectionView;
-
-
-
- UILabel *lbCancel = [UILabel new];
- lbCancel.font = [UIFont systemFontOfSize:14.0];
- lbCancel.textAlignment = NSTextAlignmentCenter;
- UITapGestureRecognizer *tapLbCancel =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onCancel:)];
- [lbCancel addGestureRecognizer:tapLbCancel];
- lbCancel.text = ASLocalizedString(@"取消");
- lbCancel.textColor = [UIColor whiteColor];
- lbCancel.backgroundColor = kCancelBtnColor;
- [self addSubview:lbCancel];
- _lbCancel = lbCancel;
-
- }
- - (void)layoutUI{
-
- CGFloat itemCount = self.itemNameArray.count;
- NSParameterAssert(itemCount);
-
- //行数
- CGFloat rows = ceilf(itemCount / 4);
-
-
- //item垂直方向的高度
- CGFloat itemVH = (kScreenW - kLSpace - kRSpace)/4;
-
- //item所有行的行高
- CGFloat collectionViewH = (itemVH *rows + (rows-1)*kLineSpace + kTSpace + kBSpace);
-
-
- //取消按钮高度
- _cancelBtnH = _cancelBtnH? _cancelBtnH:kCancelBtnH;
-
- //弹出视图高度
- _popViewH = collectionViewH+kCancelBtnH+kTitleH;
- // self.lbTitle.backgroundColor = [UIColor greenColor];
-
- __weak __typeof(self)weakSelf = self;
- [self.viewTitle mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(weakSelf);
- make.height.mas_equalTo(kTitleH);
- }];
-
- [self.lbTitle mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(weakSelf.viewTitle).offset(15);
- make.bottom.right.equalTo(weakSelf.viewTitle);
- }];
-
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(weakSelf.viewTitle.mas_bottom);
- make.left.right.equalTo(weakSelf);
- make.bottom.equalTo(weakSelf.lbCancel.mas_top);
- make.height.mas_equalTo(collectionViewH);
- }];
-
- [self.lbCancel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(weakSelf);
- make.bottom.equalTo(weakSelf).offset(weakSelf.popViewH);
- make.height.mas_equalTo(weakSelf.cancelBtnH);
- }];
-
- [self.viewBG mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(weakSelf);
- }];
- }
- #pragma mark - Lazy Load
- - (NSDictionary *)dictConfig{
- if (!_dictConfig) {
- _textColor = _textColor?_textColor:kTextColor;
- _maskColor = _maskColor?_maskColor:kMaskBGColor;
- _popViewBGColor = _popViewBGColor?_popViewBGColor:kPopViewBGColor;
- _cancelBtnColor = _cancelBtnColor?_cancelBtnColor:kCancelBtnColor;
- _dictConfig = @{
-
- @"fontSize":@(_fontSize),
-
- @"textColor":_textColor,
- @"maskColor":_maskColor,
- @"popViewBGColor":_popViewBGColor,
- @"cancelBtnColor":_cancelBtnColor,
- };
-
- __weak __typeof(self)weakSelf = self;
- if (_cancelBtnH) {
- [self.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(weakSelf.cancelBtnH);
- }];
- }
-
- }
- return _dictConfig;
- }
- - (NSMutableArray *)itemHiddenArray{
- if (!_itemHiddenArray) {
- _itemHiddenArray = [NSMutableArray new];
- }
- return _itemHiddenArray;
- }
- #pragma mark - Setter
- - (void)setCancelBtnColor:(UIColor *)cancelBtnColor{
- _cancelBtnColor = cancelBtnColor;
- if (_cancelBtnColor) {
- self.lbCancel.backgroundColor = _cancelBtnColor;
- }
- }
- - (void)setPopViewBGColor:(UIColor *)popViewBGColor{
- _popViewBGColor = popViewBGColor;
- if (_popViewBGColor) {
- self.collectionView.backgroundColor = _popViewBGColor;
- }
-
- }
- - (void)setMaskColor:(UIColor *)maskColor{
- _maskColor = maskColor;
- if (_maskColor) {
- self.viewBG.backgroundColor = _maskColor;
- }
- }
- - (void)setShareType:(ShareType)shareType{
- _shareType = shareType;
-
- switch (_shareType) {
- case ShareType_WorkGroup:
- {
-
- self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO),@(NO)]];
- // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
- // self.itemHiddenArray[0] = @(YES);
- // self.itemHiddenArray[1] = @(YES);
- // }
-
- self.iconNameArray = @[@"workgroup_sharetowechatfriendcircle",@"workgroup_sharetowechatfriend",@"workgroup_sharetopikewaydynamic"];
- self.itemNameArray = @[ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友"),ASLocalizedString(@"动态")];
- }
- break;
- case ShareType_Card:
- {
- self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO)]];
- // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
- // self.itemHiddenArray[0] = @(YES);
- // self.itemHiddenArray[1] = @(YES);
- // }
-
- self.iconNameArray = @[@"workgroup_sharetowechatfriendcircle",@"workgroup_sharetowechatfriend"];
- self.itemNameArray = @[ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友")];
- }
- break;
- case ShareType_News:
- {
-
- self.itemHiddenArray = [NSMutableArray arrayWithArray:@[@(NO),@(NO),@(NO)]];
- // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_Weixin]){
- // self.itemHiddenArray[1] = @(YES);
- // self.itemHiddenArray[2] = @(YES);
- // }
- // if (![[YHSocialShareManager sharedInstance]isInstallClient:YHSharePlatform_QQ]) {
- // self.itemHiddenArray[0] = @(YES);
- // }
-
- self.iconNameArray = @[@"common_img_qq",@"common_img_timeline",@"common_img_wechatFri"];
- self.itemNameArray = @[@"qq",ASLocalizedString(@"朋友圈"),ASLocalizedString(@"微信好友")];
- }
- break;
- default:
- break;
- }
- }
- - (void)setItemNameArray:(NSArray *)itemNameArray{
- _itemNameArray = itemNameArray;
- [self layoutUI];
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- return self.itemNameArray.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- ColCellForShareItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ColCellForShareItem class]) forIndexPath:indexPath];
- if (indexPath.row < self.iconNameArray.count) {
- cell.imgvIcon.image = [UIImage imageNamed:self.iconNameArray[indexPath.row]];
- }
- if (indexPath.row < self.itemNameArray.count) {
- cell.lbItemName.text = self.itemNameArray[indexPath.row];
- }
- if (indexPath.row < self.itemHiddenArray.count) {
- BOOL hidden = [self.itemHiddenArray[indexPath.row] boolValue];
- cell.hidden = hidden;
- }
- cell.dictConfig = self.dictConfig;
- return cell;
- }
- #pragma mark - UICollectionViewDelegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- if (_dBlock) {
- _dBlock(NO,indexPath.row);
- }
- [self hide];
- }
- #pragma mark - Action
- - (void)onCancel:(id)sender{
- [self hide];
- if (_dBlock) {
- _dBlock(YES,-1);
- }
- }
- #pragma mark - Public
- - (void)show{
- self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
- [self.window addSubview:self];
- //视图弹出
- __weak __typeof(self)weakSelf = self;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [weakSelf.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(weakSelf);
- }];
- [UIView animateWithDuration:0.25 animations:^{
- [weakSelf layoutIfNeeded];
- }];
- });
- }
- - (void)hide{
- __weak __typeof(self)weakSelf = self;
- [weakSelf.lbCancel mas_updateConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(weakSelf).offset(weakSelf.popViewH);
- }];
- [UIView animateWithDuration:0.25 animations:^{
- [self layoutIfNeeded];
- }completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
-
-
- }
- - (void)dismissHandler:(DismissBlock)handler{
- _dBlock = handler;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|