// // shareContentController.m // ThePaperDemo // // Created by scar1900 on 14/12/8. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "shareContent.h" #import "shareUtil.h" #define shareBtnTag 3200 @interface shareContent () { CGFloat iconPadding; CGFloat iconWidthAndHeight; CGFloat iconBtnHeight; CGFloat contentHeight; CGFloat iconBtnPadding; } @property(nonatomic, strong)UIView *backView; @property(nonatomic, strong)UIImageView *snapView; @property(nonatomic, strong)UIButton* senderButton; @property(nonatomic, strong)NSMutableArray *shareBtnList; @end @implementation shareContent @synthesize snapView; @synthesize delegate; @synthesize senderButton; @synthesize shareBtnList; @synthesize contentBO; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.isSimpleShareView = YES; self.backgroundColor = [UIColor clearColor]; [self addSubview:self.backView]; self.clipsToBounds = YES; if (IS_IPHONE_6P) { iconPadding = 40; }else if (IS_IPHONE_6){ iconPadding = 43; }else { iconPadding = 30; } iconWidthAndHeight = (rect_screen.size.width-5*iconPadding)/4; iconBtnHeight = iconWidthAndHeight + 13/2 + 16; iconBtnPadding = iconPadding - 13; contentHeight = 2*iconPadding + iconBtnHeight*3 + iconBtnPadding*2; [self initShareBtnListAndLabel]; [self addShareBtnInContent]; } return self; } - (void)presentShareContentInView:(UIView*)view sender:(id)sender{ [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; self.senderButton = sender; self.senderButton.selected = YES; self.snapView = nil; UIToolbar *toolBar = [[UIToolbar alloc]init]; if ([toolBar respondsToSelector:@selector(setBarTintColor:)]) { self.snapView = [[UIImageView alloc]initWithFrame:self.bounds]; self.backView.backgroundColor = [UIColor clearColor]; self.snapView.userInteractionEnabled = YES; self.snapView.image = customLightDarkBlurSnapshotFromView(view).image; self.snapView.alpha = 0; } toolBar = nil; [UIView animateWithDuration:0.25 animations:^{ if (self.snapView) { [self addSubview:snapView]; self.snapView.alpha = 1; [self.snapView addGestureRecognizer:tap]; }else { self.backView.alpha = 0.8; [self.backView addGestureRecognizer:tap]; } [view addSubview:self]; } completion:^(BOOL finished) { [self addSubview:self.shareContentView]; [CoreAnimationEffect animationPushUp:self.shareContentView]; [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }]; } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:self.bounds]; _backView.alpha = 0; _backView.backgroundColor = [UIColor colorWithHexString:@"0x3e3c3d"]; } return _backView; } - (UIView *)shareContentView { if (!_shareContentView) { _shareContentView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds)-contentHeight, CGRectGetWidth(self.bounds), contentHeight)]; _shareContentView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; } return _shareContentView; } - (void)tap:(id)sender { [self dismissShareContent]; } - (void)initShareBtnListAndLabel { self.shareBtnList = [NSMutableArray arrayWithCapacity:12]; for (int i=0;i<12;i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.tag = shareBtnTag + i; [self setShareBtn:button tagNum:i]; [self setButtonHiligthAndDisableColor:button]; button.layer.cornerRadius = 2.0f; button.clipsToBounds = YES; if (i<4) { button.frame = CGRectMake(iconPadding+i*(iconWidthAndHeight+iconPadding), iconPadding, iconWidthAndHeight, iconWidthAndHeight); }else if (i<8) { button.frame = CGRectMake(iconPadding+(i-4)*(iconWidthAndHeight+iconPadding), iconPadding+iconBtnHeight+iconBtnPadding, iconWidthAndHeight, iconWidthAndHeight); }else { button.frame = CGRectMake(iconPadding+(i-8)*(iconWidthAndHeight+iconPadding), iconPadding+2*iconBtnHeight+2*iconBtnPadding, iconWidthAndHeight, iconWidthAndHeight); } [self.shareBtnList addObject:button]; } } - (void)setShareBtn:(UIButton*)button tagNum:(int)num{ UIImageView *btnImageView = [[UIImageView alloc]init]; switch (num) { case 0: btnImageView.image = Image(@"login/shareWeChat.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x77b14d"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(weChatShare:) forControlEvents:UIControlEventTouchUpInside]; if (![shareUtil isHaveWeChatClient]) { button.enabled = NO; }else { button.enabled = YES; } break; case 1: btnImageView.image = Image(@"login/shareWeChatFriend.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x77b14d"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(friendGroupShare:) forControlEvents:UIControlEventTouchUpInside]; if (![shareUtil isHaveWeChatClient]) { button.enabled = NO; }else { button.enabled = YES; } break; case 2: btnImageView.image = Image(@"login/shareSinaWeiBo.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0xcf803d"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(sinaShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 3: btnImageView.image = Image(@"login/shareQQZone.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0xd7bf37"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(qzoneShare:) forControlEvents:UIControlEventTouchUpInside]; if (![shareUtil isHaveQQClient]) { button.enabled = NO; }else { button.enabled = YES; } break; case 4: btnImageView.image = Image(@"login/shareQQFriend.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x5da3e9"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(qqShare:) forControlEvents:UIControlEventTouchUpInside]; if (![shareUtil isHaveQQClient]) { button.enabled = NO; }else { button.enabled = YES; } break; case 5: btnImageView.image = Image(@"login/shareTencentWeiBo.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x83b6d3"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(tencentShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 6: btnImageView.image = Image(@"login/shareEvernote.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x75b249"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(evernoteShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 7: btnImageView.image = Image(@"login/shareYouDao.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x83b6d3"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(youdaoShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 8: btnImageView.image = Image(@"login/shareDouban.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x77b24e"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(doubanShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 9: btnImageView.image = Image(@"login/sharePocket.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0xce4358"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(pocketShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 10: btnImageView.image = Image(@"login/shareSys.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0x6b96cc"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(systemShare:) forControlEvents:UIControlEventTouchUpInside]; break; case 11: btnImageView.image = Image(@"login/shareCopy.png"); [button setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:@"0xb4dfdc"]) forState:UIControlStateNormal]; [button addTarget:self action:@selector(copyShare:) forControlEvents:UIControlEventTouchUpInside]; break; default: break; } CGSize size = btnImageView.image.size; [button addSubview:btnImageView]; [btnImageView makeConstraints:^(MASConstraintMaker *make) { make.size.equalTo(size); make.center.equalTo(button.center); }]; } - (void)setButtonHiligthAndDisableColor:(UIButton*)btn { [btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONSELECTBACK]) forState:UIControlStateHighlighted]; [btn setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateDisabled]; } - (void)addShareBtnInContent { if (self.shareBtnList.count > 0) { [self.shareBtnList enumerateObjectsUsingBlock:^(UIButton* btn, NSUInteger idx, BOOL *stop) { [self.shareContentView addSubview:btn]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(btn.center.x-117/4, CGRectGetMaxY(btn.frame)+13/2, 117/2, 16)]; label.font = appFont([iphoneLineSpaceAndParaSpace returnLevel1plus5FontSize], NO); label.textColor = [UIColor colorWithHexString:TextBlack]; label.textAlignment = NSTextAlignmentCenter; switch (idx) { case 0: label.text = @"微信好友"; break; case 1: label.text = @"微信朋友圈"; break; case 2: label.text = @"新浪微博"; break; case 3: label.text = @"QQ空间"; break; case 4: label.text = @"QQ好友"; break; case 5: label.text = @"腾讯微博"; break; case 6: label.text = @"印象笔记"; break; case 7: label.text = @"有道云笔记"; break; case 8: label.text = @"豆瓣网"; break; case 9: label.text = @"pocket"; break; case 10: label.text = @"系统分享"; break; case 11: label.text = @"复制链接"; break; default: break; } CGFloat width = [label sizeThatFits:CGSizeMake(CGFLOAT_MAX, 16)].width; label.frame = CGRectMake(btn.center.x-width/2, CGRectGetMaxY(btn.frame)+13/2, width, 16); [self.shareContentView addSubview:label]; }]; } } - (void)dismissShareContent { self.senderButton.selected = NO; if (self.isSimpleShareView) { [UIView animateWithDuration:0.25 animations:^{ self.shareContentView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), CGRectGetHeight(self.shareContentView.bounds)); self.snapView.alpha = 0; self.backView.alpha = 0; } completion:^(BOOL finished) { [self.shareContentView removeFromSuperview]; [self.backView removeFromSuperview]; [self.snapView removeFromSuperview]; [self removeFromSuperview]; if ([self.delegate respondsToSelector:@selector(didDismissShareContent:)]) { [self.delegate didDismissShareContent:self]; } }]; }else { if ([self.delegate respondsToSelector:@selector(didDismissShareContent:)]) { [self.delegate didDismissShareContent:self]; } } } #pragma mark - share event - (void)weChatShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:weChatSessionShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"weChatShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"WEIXIN"]; }]; [self dismissShareContent]; } - (void)friendGroupShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:weChatTimeLineShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"friendGroupShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"WEIXIN"]; }]; [self dismissShareContent]; } - (void)sinaShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:sinaWeiBoShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"sinaShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"SINA"]; }]; [self dismissShareContent]; } - (void)pocketShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:nil shareType:pocketShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"pocketShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"POCKET"]; }]; [self dismissShareContent]; } - (void)qzoneShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:QQZoneShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"qzoneShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; [self dismissShareContent]; } - (void)qqShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:QQFriendShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"qqShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; [self dismissShareContent]; } - (void)tencentShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:TencentWeboShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"tencentShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; [self dismissShareContent]; } - (void)evernoteShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:evernoteShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"evernoteShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"YINXIANG"]; }]; [self dismissShareContent]; } - (void)youdaoShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:youdaoShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"youdaoShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"YOUDAO"]; }]; [self dismissShareContent]; } - (void)doubanShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:doubanShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"doubanShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"DOUBAN"]; }]; [self dismissShareContent]; } - (void)renrenShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:renrenShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"renrenShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"RENREN"]; }]; [self dismissShareContent]; } - (void)emailShare:(UIButton*)btn { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:emailShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"emailShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; [self dismissShareContent]; } - (void)copyShare:(UIButton*)btn { [MobClick event:@"73"]; [self dismissShareContent]; __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:copyType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"copyShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; } - (void)systemShare:(UIButton*)btn { [self dismissShareContent]; __weak typeof(self) weakSelf = self; [shareUtil instance].baseSharedController = self.baseController; [shareUtil shareNews:self.contentBO.shareUrl imageUrl:self.contentBO.sharePic title:self.contentBO.name summary:self.contentBO.summary shareType:systemShareType shareStyle:self.sharestyle completion:^(BOOL isSuccess) { TPLOG(@"systemShare Success!"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; } - (void)completion:(BOOL)isSuccess andWeiboType:(NSString*)weiboType { if (isSuccess) { NSLog(@"upload sharelog"); NSDictionary *dic = @{@"weiboType": weiboType, @"shareType": @"3", @"objectType": @"1"}; [Remote doJsonActionWithBlock:1 requestUrl:shareLogURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { if (success) NSLog(@"upload success"); else NSLog(@"upload failed"); }]; } else NSLog(@"share failed"); } @end