1、shardSDK的分享面板我们不要说他丑了,虽然它确实不好看。那么自己动手丰衣足食,那就果断自定义一个吧:(支持QQ\微信\QQZone\朋友圈\收藏,弹出和收回动画、点击背景隐藏)
//
// ShareCustom.m
// mplus
//
// Created by Axiba on 16/3/27.
// Copyright © 2016年 Zero. All rights reserved.
//
#import "ShareCustom.h"
#import "UIView+Extension.h"
#import "NSString+Extension.h"
#import <QuartzCore/QuartzCore.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
@implementation ShareCustom
static NSMutableDictionary *_publishContent;//类方法中的全局变量这样用(类型前面加static)
+(void)shareWithContent:(NSMutableDictionary *)publishContent
{
_publishContent = publishContent;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//背景颜色
UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight)];
blackView.backgroundColor = XBAColor(0, 0, 0, 0.6);
blackView.tag = 440;
UITapGestureRecognizer *blackGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelPanGesture)];
[blackView addGestureRecognizer:blackGestureRecognizer];
blackView.userInteractionEnabled = YES;
[window addSubview:blackView];
//分享面板
UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0, fDeviceHeight-300*KWidth_Scale, fDeviceWidth, 300*KWidth_Scale)];
shareView.backgroundColor = XBColor(241, 244, 245);
shareView.tag = 441;
[window addSubview:shareView];
//顶部的文字
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"分享给好友";
CGSize createSize = [titleLabel.text sizeWithTextFont:[UIFont systemFontOfSize:16*KWidth_Scale]];
titleLabel.frame = (CGRect){{0, 16*KWidth_Scale}, createSize};
titleLabel.centerX = shareView.centerX;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:16*KWidth_Scale];
titleLabel.textColor = XBColor(51, 51, 51);
titleLabel.backgroundColor = [UIColor clearColor];
[shareView addSubview:titleLabel];
//文字左\右边的线条
UIView *nothingImg = [[UIView alloc] init];
nothingImg.backgroundColor = XBColor(220,220,220);
nothingImg.height = 0.5;
nothingImg.width = (shareView.width - (30 + 40)*KWidth_Scale - titleLabel.width)/2;
nothingImg.y = titleLabel.centerY;
nothingImg.x = 20*KWidth_Scale;
[shareView addSubview:nothingImg];
UIView *nothingRImg = [[UIView alloc] init];
nothingRImg.backgroundColor = XBColor(220,220,220);
nothingRImg.height = 0.5;
nothingRImg.width = (shareView.width - (30 + 40)*KWidth_Scale - titleLabel.width)/2;
nothingRImg.y = titleLabel.centerY;
nothingRImg.x = shareView.width - 20*KWidth_Scale - nothingRImg.width;
[shareView addSubview:nothingRImg];
NSArray *btnImages = @[@"分享-QQ空间", @"分享_qq_icon", @"分享_微信_icon", @"分享_朋友圈_icon", @"分享-复制链接"];
NSArray *btnTitles = @[@"QQ空间", @"QQ", @"微信", @"朋友圈", @"复制"];
CGFloat textlbH = 0;
for (NSInteger i=0; i < btnImages.count; i++) {
UIImage *img = [UIImage imageNamed:btnImages[i]];
UIButton *button = [[UIButton alloc] init];
[button setImage:img forState:UIControlStateNormal];
CGFloat top = 0.0f;
if (i<4) {
top = 20*KWidth_Scale;
}else{
top = (20 + img.size.width + 28)*KWidth_Scale + textlbH;
}
CGFloat margin = (mainSize.width - img.size.width*KWidth_Scale*4)/5;
button.x = (i%4 + 1)*margin + (i%4)*img.size.width*KWidth_Scale;
button.y = CGRectGetMaxY(titleLabel.frame) + top;
button.width = img.size.width*KWidth_Scale;
button.height = img.size.height*KWidth_Scale;
button.tag = 331+i;
[button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:button];
UILabel *createlb = [[UILabel alloc]init];
CGSize createSize = [btnTitles[i] sizeWithTextFont:[UIFont systemFontOfSize:12]];
createlb.frame = (CGRect){{0, CGRectGetMaxY(button.frame) + 8*KWidth_Scale}, createSize};
createlb.centerX = button.centerX;
createlb.textAlignment = NSTextAlignmentCenter;
createlb.text = btnTitles[i];
createlb.textColor = XBAColor(51, 51, 51, 1);
createlb.font = [UIFont systemFontOfSize:12];
textlbH = createlb.height;
[shareView addSubview:createlb];
}
UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, shareView.height-44*KWidth_Scale, mainSize.width, 44*KWidth_Scale)];
[cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancleBtn setBackgroundColor:XBColor(255, 255, 255)];
[cancleBtn setTitleColor:XBAColor(51, 51, 51, 1) forState:UIControlStateNormal];
cancleBtn.titleLabel.font = [UIFont systemFontOfSize:17*KWidth_Scale];
cancleBtn.tag = 339;
[cancleBtn addTarget:self action:@selector(cancelWithRemoveShareView) forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:cancleBtn];
//为了弹窗不那么生硬,这里加了个简单的动画
shareView.frame = CGRectMake(0, mainSize.height, shareView.width, shareView.height);
[UIView animateWithDuration:0.35f animations:^{
shareView.frame = CGRectMake(0, mainSize.height-shareView.height, shareView.width, shareView.height);
}];
}
+(void)shareBtnClick:(UIButton *)btn
{
//移除分享面板
[self cancelWithRemoveShareView];
int shareType = 0;
NSMutableDictionary *publishContent = _publishContent;
switch (btn.tag) {
case 331:
{
shareType = SSDKPlatformSubTypeQZone;
}
break;
case 332:
{
shareType = SSDKPlatformSubTypeQQFriend;
}
break;
case 333:
{
shareType = SSDKPlatformSubTypeWechatSession;
}
break;
case 334:
{
shareType = SSDKPlatformSubTypeWechatTimeline;
}
break;
case 335:
{
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:
{
NSString *resulTitle = [NSString stringWithFormat:(shareType == SSDKPlatformTypeCopy) ? @"复制成功" : @"分享成功"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:resulTitle
message:nil
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView show];
break;
}
case SSDKResponseStateFail:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
break;
}
default:
break;
}
}];
}
+(void)cancelWithRemoveShareView{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *blackView = [window viewWithTag:440];
UIView *shareView = [window viewWithTag:441];
//为了移除弹窗不那么生硬,这里加了个简单的动画
shareView.frame = CGRectMake(0, mainSize.height-shareView.height, shareView.width, shareView.height);
[UIView animateWithDuration:0.35f animations:^{
shareView.frame = CGRectMake(0, mainSize.height, shareView.width, shareView.height);
} completion:^(BOOL finished) {
[shareView removeFromSuperview];
[blackView removeFromSuperview];
}];
}
+(void)cancelPanGesture{
[self cancelWithRemoveShareView];
}
@end
2、那么调用这个自定义类的方式就是:
-(void)shareURL:(UIButton *)button{
NSArray* imageArray = @[[UIImage imageNamed:@"logo-图标"]];
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"山重水复疑无路,柳暗花明还不行"
images:imageArray
url:[NSURL URLWithString:@"http://www.jianshu.com/p/f3d62ad7a2bb"]
title:@"山重水复疑无路,柳暗花明还不行"
type:SSDKContentTypeAuto];
//调用自定义分享
[ShareCustom shareWithContent:shareParams];
}