shareSDK自定义分享界面UI

如果要用shareSDK自带的UI,分享界面是不能修改的,只能更改分享平台的小图标和小图标下面的文字,如果非要更改分享界面,只能自己画UI,然后调用shareSDK的无UI分享方法。shareSDK技术支持给了一个链接,让我参考,点击进入,于是我自定义了一个分享类,然后只需要在分享事件的方法中构建分享内容publishContent,

?
1
传入[ShareCustom shareWithContent:publishContent];即可

 

下面是设计给的标注图以及我做出来的效果图:

\\

 

下面是实现代码:(我自定义了一个专门分享的类)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//下面是.h文件
//  Copyright (c) 2015年 yanhong. All rights reserved.
//
 
# import <foundation foundation.h= "" >
/*
     自定义的分享类,使用的是类方法,其他地方只要 构造分享内容publishContent就行了
  */
@interface ShareCustom : NSObject
 
+( void )shareWithContent:(id)publishContent; //自定义分享界面
 
@end
</foundation>

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//下面是.m文件
//  Copyright (c) 2015年 yanhong. All rights reserved.
//
 
# import "ShareCustom.h"
# import <quartzcore quartzcore.h= "" >
# import <sharesdk sharesdk.h= "" >
//设备物理大小
#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" , @ "IOS-qq@2x.png" , @ "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分享类型,
      */
     [ShareSDK showShareViewWithType:shareType container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<issplatformshareinfo> statusInfo, id<icmerrorinfo> 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
</icmerrorinfo></issplatformshareinfo></sharesdk></quartzcore>

为了方便快速移植,我这里贴一段构建分享内容的代码

 

 

?
1
2
3
4
5
6
7
8
9
10
//构造分享内容
     id<isscontent> publishContent = [ShareSDK content:str1
                                        defaultContent:str1
                                                 image:nil
                                                 title:@ " "
                                                   url:urlString
                                           description:str1
                                             mediaType:SSPublishContentMediaTypeText];
     //调用自定义分享
     [ShareCustom shareWithContent:publishContent];</isscontent>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值