ShareSDK自定义分享平台UI
opengcy
8年前
<p>用过ShareSDK三方分享的估计都知道,其实它自带的UI挺丑的。而实际开发过程中,我们想要各种各样的UI样式 ,所以它自带的样式就没办法满足我们的需求了,这时候,需要我们通过自定义的UI样式,来达到我们的开发需求。好在ShareSDK给我们提供了自定义UI的API,这样我们才能够在使用自定义UI的同时,也能使用它的分享接口。</p> <p>那么,我们实现自定义UI的原理是什么呢?</p> <p>首先,我们知道,ShareSDK是通过SSDKPlatformType来判别分享的平台的,所以,我们自定义UI样式的时候,只需要知道我们当前点击的分享平台是什么,通过调用其类方法: + (void)share:(SSDKPlatformType)platformType parameters:(NSMutableDictionary *)parameters onStateChanged:(SSDKShareStateChangedHandler)stateChangedHandler ;便可实现相应平台的分享。</p> <p>接下来,我们写一个类,实现自定义UI的方法,这样我们需要分享的时候,直接用这个类调相应的方法即可。</p> <p>1、创建 ShareSDKMethod.h 类型</p> <p>写一个类方法:</p> <p>+(void)shareToPlatformsWithNetImage : (NSString *)netImageUrl ShareContent : (NSString *)shareContent ShareLink : (NSString *)shareLink ShareTitle : (NSString *)shareTitle;</p> <p>通过该方法将要分享的参数传递过来。</p> <p>2、在 ShareSDKMethod.m 实现类方法以及自定义UI</p> <p>引入头文件</p> <pre> <code class="language-objectivec">#import <ShareSDK/ShareSDK.h> #import <ShareSDKUI/ShareSDK+SSUI.h></code></pre> <p>(1)实现类方法</p> <pre> <code class="language-objectivec">+(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]; }</code></pre> <p>(2)、自定义UI的实现以及分享平台的点击事件</p> <pre> <code class="language-objectivec">+(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]; }</code></pre> <p>3、在分享按钮的点击事件中调用类方法</p> <p>+(void)shareToPlatformsWithNetImage:(NSString *)netImageUrl ShareContent:(NSString *)shareContent ShareLink:(NSString *)shareLink ShareTitle:(NSString *)shareTitle</p> <p>传递要分享的参数:</p> <pre> <code class="language-objectivec">[ShareSDKMethod shareToPlatformsWithNetImage:@"图片的URL" ShareContent:@"分享的内容" ShareLink:@"分享的链接" ShareTitle:@"分享标题"];</code></pre> <p>最后,效果如图</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/55ba4a6217a8e960cfc0ffd1b56e9411.png"></p> <p style="text-align:center">自定义分享控制面板</p> <p> </p> <p>来自:http://www.jianshu.com/p/e39753bc5060</p> <p> </p>