用過(guò)ShareSDK三方分享的估計(jì)都知道栈雳,其實(shí)它自帶的UI挺丑的。而實(shí)際開(kāi)發(fā)過(guò)程中绊寻,我們想要各種各樣的UI樣式 ,所以它自帶的樣式就沒(méi)辦法滿足我們的需求了悬秉,這時(shí)候澄步,需要我們通過(guò)自定義的UI樣式,來(lái)達(dá)到我們的開(kāi)發(fā)需求和泌。好在ShareSDK給我們提供了自定義UI的API驮俗,這樣我們才能夠在使用自定義UI的同時(shí),也能使用它的分享接口允跑。
那么,我們實(shí)現(xiàn)自定義UI的原理是什么呢搪柑?
首先聋丝,我們知道,ShareSDK是通過(guò)SSDKPlatformType來(lái)判別分享的平臺(tái)的工碾,所以弱睦,我們自定義UI樣式的時(shí)候,只需要知道我們當(dāng)前點(diǎn)擊的分享平臺(tái)是什么渊额,通過(guò)調(diào)用其類方法:+ (void)share:(SSDKPlatformType)platformType parameters:(NSMutableDictionary *)parameters onStateChanged:(SSDKShareStateChangedHandler)stateChangedHandler
;便可實(shí)現(xiàn)相應(yīng)平臺(tái)的分享况木。
接下來(lái),我們寫一個(gè)類旬迹,實(shí)現(xiàn)自定義UI的方法火惊,這樣我們需要分享的時(shí)候,直接用這個(gè)類調(diào)相應(yīng)的方法即可奔垦。
1屹耐、創(chuàng)建ShareSDKMethod.h
類型
寫一個(gè)類方法:
+(void)shareToPlatformsWithNetImage : (NSString *)netImageUrl ShareContent : (NSString *)shareContent ShareLink : (NSString *)shareLink ShareTitle : (NSString *)shareTitle;
通過(guò)該方法將要分享的參數(shù)傳遞過(guò)來(lái)。
2椿猎、在ShareSDKMethod.m
實(shí)現(xiàn)類方法以及自定義UI
引入頭文件
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
(1)實(shí)現(xiàn)類方法
+(void)shareToPlatformsWithNetImage:(NSString *)netImageUrl ShareContent:(NSString *)shareContent ShareLink:(NSString *)shareLink ShareTitle:(NSString *)shareTitle{
_shareParams=[NSMutableDictionary dictionary];
//自定義每個(gè)平臺(tái)的分享內(nèi)容
// 微信朋友圈
[_shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatSession];
// 微信好友
[_shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@", shareContent] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
// 新浪微博
[_shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"【%@】%@%@",shareTitle, shareContent, [NSURL URLWithString:shareLink]] title:shareTitle image:netImageUrl url:[NSURL URLWithString:shareLink] latitude:0 longitude:0 objectID:nil type:SSDKContentTypeAuto];
// QQ好友
[_shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformTypeQQ];
// QQ空間
[_shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeQZone];
// 復(fù)制
[_shareParams SSDKSetupCopyParamsByText:nil images:nil url:[NSURL URLWithString:shareLink] type:SSDKContentTypeAuto];
//創(chuàng)建自定義的UI樣式
[self createCustomUI];
}```
(2)惶岭、自定義UI的實(shí)現(xiàn)以及分享平臺(tái)的點(diǎn)擊事件
+(void)createCustomUI{
// 將控件加到keyWindow上
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//透明蒙層
UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
grayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
grayView.tag = 1000;
UITapGestureRecognizer *tapGrayView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelShareAction)];
[grayView addGestureRecognizer:tapGrayView];
grayView.userInteractionEnabled = YES;
[window addSubview:grayView];
//分享控制器
UIView *shareBackView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-224*FitHeight, ScreenWidth, 224*FitHeight)];
shareBackView.backgroundColor =[UIColor colorWithHex:@"#ffffff" alpha:0.95];
shareBackView.tag = 1001;
[window addSubview:shareBackView];
//分享標(biāo)題提示語(yǔ)
UILabel *shareTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44*FitHeight)];
shareTipLabel.text = @"分享";
shareTipLabel.textAlignment = NSTextAlignmentCenter;
shareTipLabel.font = [UIFont systemFontOfSize:11*FitWidth];
shareTipLabel.textColor = [UIColor colorWithHex:@"#333333"];
shareTipLabel.backgroundColor = [UIColor clearColor];
[shareBackView addSubview:shareTipLabel];
NSArray *ImageArr = @[[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e64c", 44*FitWidth, [UIColor colorWithHex:@"#5bbb53"])], [UIImage imageNamed:@"share_circle_03"],[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e660", 30*FitWidth, [UIColor whiteColor])], [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e632", 30*FitWidth, [UIColor whiteColor])], [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e637",30*FitWidth, [UIColor whiteColor])],[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e638", 30*FitWidth, [UIColor colorWithHex:@"#4d4d4d"])]];
NSArray *colorArr=@[[UIColor whiteColor],[UIColor whiteColor],[UIColor colorWithHex:@"#cd4520"],[UIColor colorWithHex:@"#3bb0ec"],[UIColor colorWithHex:@"#fad14e"],[UIColor whiteColor]];
NSArray *tincolorArr=@[[UIColor colorWithHex:@"#5bbb53"],[UIColor clearColor],[UIColor whiteColor],[UIColor whiteColor],[UIColor whiteColor],[UIColor colorWithHex:@"#4d4d4d"]];
NSArray *platformNameArr = @[@"微信", @"微信朋友圈", @"新浪微博", @"QQ好友",@"QQ空間", @"復(fù)制鏈接"];
for (NSInteger i=0; i < platformNameArr.count; i++) {
UIView *backView=[[UIView alloc] init];
if (i<3) {
backView.frame = CGRectMake((i+1)*47*FitWidth+i*44*FitWidth, 44*FitHeight, 44*FitWidth, 74*FitHeight);
}else{
backView.frame = CGRectMake((i-2)*47*FitWidth+(i-3)*44*FitWidth, 118*FitHeight, 44*FitWidth, 74*FitHeight);
}
[shareBackView addSubview:backView];
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 44*FitWidth, 44*FitHeight);
button.backgroundColor = colorArr[i];
[backView addSubview:button];
UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(-4*FitWidth, button.frame.origin.y+button.frame.size.height, 52*FitWidth, 28*FitHeight)];
[backView addSubview:titleLabel];
titleLabel.textAlignment=1;
titleLabel.font = [UIFont systemFontOfSize:10*FitWidth];
titleLabel.textColor = [UIColor colorWithHex:@"#333333"];
titleLabel.text = platformNameArr[i];
UIImage *img = ImageArr[i];
button.layer.cornerRadius=10*FitWidth;
if (i==1) {
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake((button.frame.size.width-30*FitWidth)/2, (button.frame.size.height-30*FitWidth)/2, 30*FitWidth, 30*FitHeight)];
[button addSubview:imageView];
imageView.image =[UIImage imageNamed:@"share_circle_03"];
}else{
[button setTintColor:tincolorArr[i]];
[button setImage:img forState:UIControlStateNormal];
}
button.contentMode = UIViewContentModeScaleAspectFit;
button.tag = 1005+i;
[button addTarget:self action:@selector(dicClickPlatformButton:) forControlEvents:UIControlEventTouchUpInside];
}
//取消按鈕
UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, shareBackView.frame.size.height-34*FitHeight, ScreenWidth,34*FitHeight)];
[cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancleBtn setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
cancleBtn.titleLabel.font = [UIFont systemFontOfSize:11*FitWidth];
cancleBtn.tag = 1003;
[cancleBtn addTarget:self action:@selector(removeShareView) forControlEvents:UIControlEventTouchUpInside];
[shareBackView addSubview:cancleBtn];
UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, .5)];
[cancleBtn addSubview:lineLabel];
lineLabel.backgroundColor = [UIColor colorWithHex:@"#d8d8d8"];
shareBackView.frame = CGRectMake(0, ScreenHeight, shareBackView.frame.size.width, shareBackView.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
shareBackView.frame = CGRectMake(0, ScreenHeight-shareBackView.frame.size.height, shareBackView.frame.size.width, shareBackView.frame.size.height);
}];
}
+(void)dicClickPlatformButton:(UIButton *)button
{
//移除分享面板
[self removeShareView];
int shareType = 0;
NSMutableDictionary *publishContent = _shareParams;
switch (button.tag) {
case 1005:
{
shareType =SSDKPlatformSubTypeWechatSession;
}
break;
case 1006:
{
shareType = SSDKPlatformSubTypeWechatTimeline;
}
break;
case 1007:
{
shareType = SSDKPlatformTypeSinaWeibo;
}
break;
case 1008:
{
shareType = SSDKPlatformTypeQQ;
}
break;
case 1009:
{
shareType = SSDKPlatformSubTypeQZone;
}
break;
case 1010:
{
shareType = SSDKPlatformTypeCopy;
}
break;
default:
break;
}
/*
調(diào)用shareSDK的無(wú)UI分享類型
*/
[ShareSDK share:shareType parameters:publishContent onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
switch (state) {
case SSDKResponseStateSuccess:
{
if (shareType == SSDKPlatformTypeCopy) {
NSLog(@"復(fù)制成功");
}else{ NSLog(@"分享成功");
}
break;
case SSDKResponseStateFail:
if (shareType == SSDKPlatformTypeCopy) {
NSLog(@"復(fù)制失敗");
}else{
NSLog(@"分享失敗");
}
NSLog(@"失敗:%@", error);
break;
}
default:
break;
}
}];
}
+(void)removeShareView{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *blackView = [window viewWithTag:1000];
UIView *shareView = [window viewWithTag:1001];
shareView.frame =CGRectMake(0, shareView.frame.origin.y, shareView.frame.size.width, shareView.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
shareView.frame = CGRectMake(0, ScreenHeight, shareView.frame.size.width, shareView.frame.size.height);
} completion:^(BOOL finished) {
[shareView removeFromSuperview];
[blackView removeFromSuperview];
}];
}
+(void)cancelShareAction{
[self removeShareView];
}
3犯眠、在分享按鈕的點(diǎn)擊事件中調(diào)用類方法
`
+(void)shareToPlatformsWithNetImage:(NSString *)netImageUrl ShareContent:(NSString *)shareContent ShareLink:(NSString *)shareLink ShareTitle:(NSString *)shareTitle`
傳遞要分享的參數(shù):
[ShareSDKMethod shareToPlatformsWithNetImage:@"圖片的URL" ShareContent:@"分享的內(nèi)容" ShareLink:@"分享的鏈接" ShareTitle:@"分享標(biāo)題"];
最后按灶,效果如圖
![自定義分享控制面板](http://upload-images.jianshu.io/upload_images/972702-c019d1309d1be1a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
如果想了解ShareSDK的詳細(xì)集成過(guò)程,可參考之前的文章[使用ShareSDK實(shí)現(xiàn)分享功能](http://www.reibang.com/p/30ec5803d536)