首先添加友盟的sdk及設(shè)置分享跳轉(zhuǎn)的URL types,還有設(shè)置白名單
分享界面的設(shè)置,一般都是要求文字配圖片,圖片在上,文字在下,這個(gè)網(wǎng)上有好多的方法
我是自定義控件button
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:11.0];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.64f green:0.64f blue:0.64f alpha:1.00f] forState:UIControlStateHighlighted];
}
return self;
}
//取消按鈕的高亮狀態(tài)
- (void)setHighlighted:(BOOL)highlighted
{
}
-(void)layoutSubviews {
[super layoutSubviews];
// Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width / 2;
center.y = self.imageView.frame.size.height / 2 + 15;
self.imageView.center = center;
self.imageView.size = CGSizeMake(50, 50);
//Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.imageView.frame.size.height + 20;
newFrame.size.width = self.frame.size.width;
self.titleLabel.frame = newFrame;
self.titleLabel.frame = CGRectMake(0, newFrame.origin.y, newFrame.size.width, 20);
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
然后是在視圖中設(shè)置布局,使用的是9宮格,要求的效果圖

.h
//點(diǎn)擊按鈕block回調(diào)
@property (nonatomic,copy) void(^btnClick)(NSInteger);
- (id)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArr;
.m
- (id)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArr
{
if (self = [super init]) {
self.shareBtnImgArray = imageArr;
self.shareBtnTitleArray = titleArray;
self.frame = CGRectMake(0, 0, SCREENW, SCREENH);
self.backgroundColor = ARGBColor(0.0, 0.0, 0.0, 0.3);
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
[self addGestureRecognizer:tapGesture];
[self loadUiConfig];
}
return self;
}
///? 加載9宮格的分享
- (void)loadUiConfig
{
[self addSubview:self.backGroundView];
[_backGroundView addSubview:self.cancelBtn];
for (NSInteger i = 0; i < _shareBtnImgArray.count; i++)
{
ActionButton *button = [ActionButton buttonWithType:UIButtonTypeCustom];
if (_shareBtnImgArray.count % 5 == 0) {
button.frame = CGRectMake(_backGroundView.bounds.size.width / 5 *(i % 5), (i / 5) * 100, _backGroundView.bounds.size.width / 5, 100);
}else{
button.frame = CGRectMake(_backGroundView.bounds.size.width / 5 * (i % 5), (i / 5) * 100, _backGroundView.bounds.size.width / 5, 100);
}
[button setTitle:_shareBtnTitleArray[i] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:_shareBtnImgArray[i]] forState:UIControlStateNormal];
button.tag = 200 + i;
[button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.backGroundView addSubview:button];
}
if (_shareBtnImgArray.count > 5) {
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, SCREENW, 1)];
lineView.backgroundColor = ARGBColor(215.0, 215.0, 215.0, 1.0);
[self.backGroundView addSubview:lineView];
}
[UIView animateWithDuration:0.25 animations:^{
_backGroundView.frame = CGRectMake(0, SCREENH - CGRectGetHeight(_backGroundView.frame), SCREENW, CGRectGetHeight(_backGroundView.frame));
}];
}
//
- (void)BtnClick:(UIButton *)btn
{
[self tappedCancel];
if (btn.tag < 200) {
_btnClick(btn.tag - 100);
}else{
_btnClick(btn.tag - 200);
}
}
- (void)tappedCancel
{
[UIView animateWithDuration:0.25 animations:^{
[self.backGroundView setFrame:CGRectMake(0, SCREENH, SCREENW, 0)];
self.alpha = 0;
} completion:^(BOOL finished) {
if (finished) {
[self removeFromSuperview];
}
}];
}
#pragma mark - 懶加載
- (UIView *)backGroundView
{
if (_backGroundView == nil) {
_backGroundView = [[UIView alloc] init];
_backGroundView.backgroundColor = ARGBColor(229.0, 229.0, 229.0, 1.0);
if (_shareBtnImgArray.count < 5) {
_backGroundView.frame = CGRectMake(0, SCREENH, SCREENW, 150);
}else{
NSInteger index;
if (_shareBtnTitleArray.count % 5 == 0) {
index =_shareBtnTitleArray.count / 5;
}else{
index = _shareBtnTitleArray.count / 5 + 1;
}
_backGroundView.frame = CGRectMake(0, SCREENH, SCREENW, 50 + 100 * index );
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noTap)];
[_backGroundView addGestureRecognizer:tapGesture];
}
return _backGroundView;
}
- (void)noTap
{}
- (UIButton *)cancelBtn
{
if (_cancelBtn == nil) {
_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_backGroundView.frame) - 50, CGRectGetWidth(_backGroundView.frame), 50);
[_cancelBtn setTitle:NSLocalizedString(@"button_cancel", nil) forState:UIControlStateNormal];
_cancelBtn.backgroundColor = [UIColor whiteColor];
[_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelBtn;
}
分享是在工具類(lèi)中,避免appdelegate重代碼太多,有點(diǎn)小強(qiáng)迫
/**
*? 分享到各個(gè)平臺(tái)
*
*? @param PlatformName? 平臺(tái)名稱(chēng),在試圖控制器里配置,具體哪些平臺(tái)參考UMSocialSnsPlatformManager.h
*? @param text? ? ? ? ? 要分享的文本信息
*? @param description? ? 要分享的描述信息
*? @param image? ? ? ? ? 分享的圖片信息(可以傳NSData或UImage)
*? @param url? ? ? ? ? ? 要分享的網(wǎng)址
*? @param viewController 從哪個(gè)視圖控制器傳過(guò)來(lái)的(一般直接寫(xiě)self)
*/
+ (void)shareToSocialPlatform:(id)PlatformName WithTitle:(NSString *)text? andDescription:(NSString *)description andImage:(id)image andUrl:(NSString *)url showOn:(UIViewController *)viewController;
在.m中實(shí)現(xiàn)
+ (void)shareToSocialPlatform:(id)PlatformName WithTitle:(NSString *)text andDescription:(NSString *)description andImage:(id)image andUrl:(NSString *)url showOn:(UIViewController *)viewController
{
if ([PlatformName isEqualToString:UMShareToSina]) {
// __weak id weakSelf = viewController;
[[UMSocialControllerService defaultControllerService] setShareText:[NSString stringWithFormat:@"%@%@",text, url] shareImage:image socialUIDelegate:(id)viewController];
[UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(viewController,[UMSocialControllerService defaultControllerService],YES);
}else{
if ([PlatformName isEqualToString:UMShareToQQ]){
[UMSocialData defaultData].extConfig.qqData.url = url;
}else if ([PlatformName isEqualToString:UMShareToQzone]){
[UMSocialData defaultData].extConfig.qzoneData.url = url;
}else if ([PlatformName isEqualToString:UMShareToWechatSession]){
[UMSocialData defaultData].extConfig.wechatSessionData.url = url;
}else if ([PlatformName isEqualToString:UMShareToWechatTimeline]){
[UMSocialData defaultData].extConfig.wechatTimelineData.url = url;
}
[UMSocialData defaultData].extConfig.title = text;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[PlatformName] content:description image:image location:nil urlResource:nil presentedController:viewController completion:^(UMSocialResponseEntity* response) {
if (response.responseCode == UMSResponseCodeSuccess) {
}
}];
}
}
都具備了,直接在控制器中實(shí)現(xiàn)分享,由于項(xiàng)目需要實(shí)現(xiàn)語(yǔ)言國(guó)際化,所以titleArr使用的是語(yǔ)言包,有些鍵值不是分享的所以需要另外做判斷
shareArray = @[
@{@"titleArr":@[NSLocalizedString(@"menu_share_group", nil), NSLocalizedString(@"menu_share_friend", nil), NSLocalizedString(@"menu_share_weibo", nil), NSLocalizedString(@"menu_share_qq", nil), NSLocalizedString(@"menu_share_qzone", nil), NSLocalizedString(@"menu_copy_url", nil), NSLocalizedString(@"menu_add_reward", nil), NSLocalizedString(@"menu_add_comm", nil), NSLocalizedString(@"menu_ignore_meet", nil), NSLocalizedString(@"menu_delete_ask", nil)]},
@{@"imageArr":@[@"朋友圈.png", @"微信好友.png", @"微博.png", @"QQ.png", @"QQ空間.png", @"復(fù)制鏈接.png", @"追加懸賞.png", @"添加備注.png", @"省掉見(jiàn)面.png", @"刪除.png"]},
@{@"platformArr":@[UMShareToWechatTimeline,UMShareToWechatSession, UMShareToSina, UMShareToQQ,UMShareToQzone,@"lianJie",@"xuanShang",@"beiZhu",@"jianMian",@"shanChu"]}
];
}
shareArray = [CJUMengShare registerInstalled:shareArray];
ActionSheetView *shareView = [[ActionSheetView alloc]initWithShareHeadOprationWith:shareArray[0][@"titleArr"] andImageArry:shareArray[1][@"imageArr"]];
__weak id weakSelf = self;
[shareView setBtnClick:^(NSInteger btnTag) {
[weakSelf shareWithTitle:btnTag];
}];
[self.view addSubview:shareView];
// ?下面的方法是根據(jù)tag值來(lái)做相應(yīng)的事情,因?yàn)橐晥D中除了有分享還有別的功能邏輯,所以在下面判斷另行處理了
- (void)shareWithTitle:(NSInteger)btnTag
{