ShareSDK自定義分享平臺(tái)UI

用過(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)


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末筐咧,一起剝皮案震驚了整個(gè)濱河市鸯旁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖羡亩,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摩疑,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡畏铆,警方通過(guò)查閱死者的電腦和手機(jī)雷袋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)辞居,“玉大人楷怒,你說(shuō)我怎么就攤上這事⊥咴睿” “怎么了鸠删?”我有些...
    開(kāi)封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)贼陶。 經(jīng)常有香客問(wèn)我刃泡,道長(zhǎng),這世上最難降的妖魔是什么碉怔? 我笑而不...
    開(kāi)封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任烘贴,我火速辦了婚禮,結(jié)果婚禮上撮胧,老公的妹妹穿的比我還像新娘桨踪。我一直安慰自己,他們只是感情好芹啥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布锻离。 她就那樣靜靜地躺著,像睡著了一般墓怀。 火紅的嫁衣襯著肌膚如雪汽纠。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天捺疼,我揣著相機(jī)與錄音疏虫,去河邊找鬼。 笑死啤呼,一個(gè)胖子當(dāng)著我的面吹牛卧秘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播官扣,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼翅敌,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了惕蹄?” 一聲冷哼從身側(cè)響起蚯涮,我...
    開(kāi)封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤治专,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后遭顶,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體张峰,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年棒旗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了喘批。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡铣揉,死狀恐怖饶深,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情逛拱,我是刑警寧澤敌厘,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站朽合,受9級(jí)特大地震影響俱两,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜曹步,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一锋华、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧箭窜,春花似錦、人聲如沸衍腥。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)婆咸。三九已至竹捉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間尚骄,已是汗流浹背块差。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留倔丈,地道東北人憨闰。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像需五,于是被迫代替她去往敵國(guó)和親鹉动。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,116評(píng)論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)宏邮、插件泽示、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • 剛開(kāi)始接觸到《斷舍離》這本書的時(shí)候缸血,一看見(jiàn)封皮上的字,我都詫異自己買錯(cuò)了書械筛,根本不是自己想象的樣子捎泻!這是個(gè)什么...
    Miss苗喵喵閱讀 610評(píng)論 2 2
  • "Where your mind goes, energy flows." day106 生理期,于是練陰瑜伽埋哟。先...
    飛天小毛女閱讀 322評(píng)論 0 1
  • 春節(jié)結(jié)束笆豁,2017年正式開(kāi)始了,這個(gè)春節(jié)定欧,小小哲過(guò)的是樂(lè)哉悠哉渔呵,瞬間發(fā)現(xiàn)小朋友也獲取了很多的新知識(shí)。 ...
    徽州小小哲閱讀 204評(píng)論 0 0