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://upload-images.jianshu.io/upload_images/972702-c019d1309d1be1a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

如果想了解ShareSDK的详细集成过程,可参考之前的文章[使用ShareSDK实现分享功能](http://www.jianshu.com/p/30ec5803d536)


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容