iOS-ShareSDK自定義分享介面UI
阿新 • • 發佈:2018-11-02
今天設計又給了幾個介面的標註,要求完善UI,其中就包括分享介面,於是我立即詢問shareSDK的技術支援(找到shareSDK的官網,然後點選企業QQ就可以詢問了),技術支援給的解釋是,如果要用shareSDK自帶的UI,分享介面是不能修改的,只能更改分享平臺的小圖示和小圖示下面的文字,如果非要更改分享介面,只能自己畫UI,然後呼叫shareSDK的無UI分享方法。shareSDK技術支援給了一個連結,讓我參考,點選進入,於是我自定義了一個分享類,然後只需要在分享事件的方法中構建分享內容publishContent,
傳入[ShareCustom shareWithContent:publishContent];即可
下面是設計給的標註圖以及我做出來的效果圖:
下面是實現程式碼:(我自定義了一個專門分享的類)
//下面是.h檔案 // Copyright (c) 2015年 yanhong. All rights reserved. // #import /* 自定義的分享類,使用的是類方法,其他地方只要 構造分享內容publishContent就行了 */ @interface ShareCustom : NSObject +(void)shareWithContent:(id)publishContent;//自定義分享介面 @end
//下面是.m檔案
// Copyright (c) 2015年 yanhong. All rights reserved.
//
#import "ShareCustom.h"
#import
#import
//裝置物理大小
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define SYSTEM_VERSION [[UIDevice currentDevice].systemVersion floatValue]
//螢幕寬度相對iPhone6螢幕寬度的比例
#define KWidth_Scale [UIScreen mainScreen].bounds.size.width/375.0f
@implementation ShareCustom
static id _publishContent;//類方法中的全域性變數這樣用(型別前面加static)
/*
自定義的分享類,使用的是類方法,其他地方只要 構造分享內容publishContent就行了
*/
+(void)shareWithContent:(id)publishContent/*只需要在分享按鈕事件中 構建好分享內容publishContent傳過來就好了*/
{
_publishContent = publishContent;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
blackView.backgroundColor = [UIColor colorWithString:@"000000" Alpha:0.85f];
blackView.tag = 440;
[window addSubview:blackView];
UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth-300*KWidth_Scale)/2.0f, (kScreenHeight-270*KWidth_Scale)/2.0f, 300*KWidth_Scale, 270*KWidth_Scale)];
shareView.backgroundColor = [UIColor colorWithString:@"f6f6f6"];
shareView.tag = 441;
[window addSubview:shareView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, shareView.width, 45*KWidth_Scale)];
titleLabel.text = @"分享到";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:15*KWidth_Scale];
titleLabel.textColor = [UIColor colorWithString:@"2a2a2a"];
titleLabel.backgroundColor = [UIColor clearColor];
[shareView addSubview:titleLabel];
NSArray *btnImages = @[@"IOS-微信@2x.png", @"IOS-朋友圈@2x.png", @" [email protected]", @"IOS-空間@2x.png", @"IOS-微信收藏@2x.png", @"IOS-微博@2x.png", @"IOS-豆瓣@2x.png", @"IOS-簡訊@2x.png"];
NSArray *btnTitles = @[@"微信好友", @"微信朋友圈", @"QQ好友", @"QQ空間", @"微信收藏", @"新浪微博", @"豆瓣", @"簡訊"];
for (NSInteger i=0; i<8; i++) {
CGFloat top = 0.0f;
if (i<4) {
top = 10*KWidth_Scale;
}else{
top = 90*KWidth_Scale;
}
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10*KWidth_Scale+(i%4)*70*KWidth_Scale, titleLabel.bottom+top, 70*KWidth_Scale, 70*KWidth_Scale)];
[button setImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal];
[button setTitle:btnTitles[i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:11*KWidth_Scale];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitleColor:[UIColor colorWithString:@"2a2a2a"] forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 15*KWidth_Scale, 30*KWidth_Scale, 15*KWidth_Scale)];
if (SYSTEM_VERSION >= 8.0f) {
[button setTitleEdgeInsets:UIEdgeInsetsMake(45*KWidth_Scale, -40*KWidth_Scale, 5*KWidth_Scale, 0)];
}else{
[button setTitleEdgeInsets:UIEdgeInsetsMake(45*KWidth_Scale, -90*KWidth_Scale, 5*KWidth_Scale, 0)];
}
button.tag = 331+i;
[button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:button];
}
UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake((shareView.width-40*KWidth_Scale)/2.0f, shareView.height-40*KWidth_Scale-18*KWidth_Scale, 40*KWidth_Scale, 40*KWidth_Scale)];
[cancleBtn setBackgroundImage:[UIImage imageNamed:@"IOS-取消@2x.png"] forState:UIControlStateNormal];
cancleBtn.tag = 339;
[cancleBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:cancleBtn];
//為了彈窗不那麼生硬,這裡加了個簡單的動畫
shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
blackView.alpha = 0;
[UIView animateWithDuration:0.35f animations:^{
shareView.transform = CGAffineTransformMakeScale(1, 1);
blackView.alpha = 1;
} completion:^(BOOL finished) {
}];
}
+(void)shareBtnClick:(UIButton *)btn
{
// NSLog(@"%@",[ShareSDK version]);
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *blackView = [window viewWithTag:440];
UIView *shareView = [window viewWithTag:441];
//為了彈窗不那麼生硬,這裡加了個簡單的動畫
shareView.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:0.35f animations:^{
shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
blackView.alpha = 0;
} completion:^(BOOL finished) {
[shareView removeFromSuperview];
[blackView removeFromSuperview];
}];
int shareType = 0;
id publishContent = _publishContent;
switch (btn.tag) {
case 331:
{
shareType = ShareTypeWeixiSession;
}
break;
case 332:
{
shareType = ShareTypeWeixiTimeline;
}
break;
case 333:
{
shareType = ShareTypeQQ;
}
break;
case 334:
{
shareType = ShareTypeQQSpace;
}
break;
case 335:
{
shareType = ShareTypeWeixiFav;
}
break;
case 336:
{
shareType = ShareTypeSinaWeibo;
}
break;
case 337:
{
shareType = ShareTypeDouBan;
}
break;
case 338:
{
shareType = ShareTypeSMS;
}
break;
case 339:
{
}
break;
default:
break;
}
/*
呼叫shareSDK的無UI分享型別,
連結地址:http://bbs.mob.com/forum.php?mod=viewthread&tid=110&extra=page%3D1%26filter%3Dtypeid%26typeid%3D34
*/
[ShareSDK showShareViewWithType:shareType container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id statusInfo, id error, BOOL end) {
if (state == SSResponseStateSuccess)
{
// NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
}
else if (state == SSResponseStateFail)
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"未檢測到客戶端 分享失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
// NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失敗,錯誤碼:%d,錯誤描述:%@"), [error errorCode], [error errorDescription]);
}
}];
}
@end
//構造分享內容
id publishContent = [ShareSDK content:str1
defaultContent:str1
image:nil
title:@" "
url:urlString
description:str1
mediaType:SSPublishContentMediaTypeText];
//呼叫自定義分享
[ShareCustom shareWithContent:publishContent];
為了方便快速移植,我這裡貼一段構建分享內容的程式碼