ShareSDK自定義分享平臺UI
用過ShareSDK三方分享的估計都知道,其實它自帶的UI挺丑的。而實際開發過程中,我們想要各種各樣的UI樣式 ,所以它自帶的樣式就沒辦法滿足我們的需求了,這時候,需要我們通過自定義的UI樣式,來達到我們的開發需求。好在ShareSDK給我們提供了自定義UI的API,這樣我們才能夠在使用自定義UI的同時,也能使用它的分享接口。
那么,我們實現自定義UI的原理是什么呢?
首先,我們知道,ShareSDK是通過SSDKPlatformType來判別分享的平臺的,所以,我們自定義UI樣式的時候,只需要知道我們當前點擊的分享平臺是什么,通過調用其類方法: + (void)share:(SSDKPlatformType)platformType parameters:(NSMutableDictionary *)parameters onStateChanged:(SSDKShareStateChangedHandler)stateChangedHandler ;便可實現相應平臺的分享。
接下來,我們寫一個類,實現自定義UI的方法,這樣我們需要分享的時候,直接用這個類調相應的方法即可。
1、創建 ShareSDKMethod.h 類型
寫一個類方法:
+(void)shareToPlatformsWithNetImage : (NSString *)netImageUrl ShareContent : (NSString *)shareContent ShareLink : (NSString *)shareLink ShareTitle : (NSString *)shareTitle;
通過該方法將要分享的參數傳遞過來。
2、在 ShareSDKMethod.m 實現類方法以及自定義UI
引入頭文件
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
(1)實現類方法
+(void)shareToPlatformsWithNetImage:(NSString *)netImageUrl ShareContent:(NSString *)shareContent ShareLink:(NSString *)shareLink ShareTitle:(NSString *)shareTitle{
_shareParams=[NSMutableDictionary dictionary];
//自定義每個平臺的分享內容
// 微信朋友圈
[_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];
// 復制
[_shareParams SSDKSetupCopyParamsByText:nil images:nil url:[NSURL URLWithString:shareLink] type:SSDKContentTypeAuto];
//創建自定義的UI樣式
[self createCustomUI];
}
(2)、自定義UI的實現以及分享平臺的點擊事件
+(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];
//分享標題提示語
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空間", @"復制鏈接"];
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;
}
/*
調用shareSDK的無UI分享類型
*/
[ShareSDK share:shareType parameters:publishContent onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
switch (state) {
case SSDKResponseStateSuccess:
{
if (shareType == SSDKPlatformTypeCopy) {
NSLog(@"復制成功");
}else{ NSLog(@"分享成功");
}
break;
case SSDKResponseStateFail:
if (shareType == SSDKPlatformTypeCopy) {
NSLog(@"復制失敗");
}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、在分享按鈕的點擊事件中調用類方法
+(void)shareToPlatformsWithNetImage:(NSString *)netImageUrl ShareContent:(NSString *)shareContent ShareLink:(NSString *)shareLink ShareTitle:(NSString *)shareTitle
傳遞要分享的參數:
[ShareSDKMethod shareToPlatformsWithNetImage:@"圖片的URL" ShareContent:@"分享的內容" ShareLink:@"分享的鏈接" ShareTitle:@"分享標題"];
最后,效果如圖
自定義分享控制面板
來自:http://www.jianshu.com/p/e39753bc5060