码迷,mamicode.com
首页 > 其他好文 > 详细

shareSDK自定义分享界面UI

时间:2015-05-27 23:04:57      阅读:637      评论:0      收藏:0      [点我收藏+]

标签:sharesdk   自定义分享界面   ios   分享   

</pre>今天,处女座的设计师又丢过来几个页面的切图,需要更改UI界面,其中就有一个是分享的UI需要调整,找到shareSDK的官网,然后点击企业QQ,咨询那边技术“我要更改shareSDK的分享界面,我该从哪里下手”,他告诉我说,除了分享平台的小图标和分享平台的文字能改,其他的比如UI是不能改的,如果用户要用share SDK的UI的话,每一行默认显示三个分享平台的小图标要改成四个都不改不了,但是他最后给我来了一句:如果实在要改的话,只能自己画UI,然后调用share SDK的无UI方法。然后他给了我一个链接,让我参考着做基本上就没问题。链接地址是:<a target=_blank target="_blank" href="http://bbs.mob.com/forum.php?mod=viewthread&tid=110&extra=page%3D1%26filter%3Dtypeid%26typeid%3D34">http://bbs.mob.com/forum.php?mod=viewthread&tid=110&extra=page%3D1%26filter%3Dtypeid%26typeid%3D34</a><p></p><p>然后我自己封装了一个类,写了一套UI,然后根据点击事件调用share SDK的无UI方法:</p><p>设计切图和我做出来后的效果如下图:</p><p><img src="http://img.blog.csdn.net/20150527173407266?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRWhvbWVfaG9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="375" height="667" alt="" /><img src="http://img.blog.csdn.net/20150527173633338?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRWhvbWVfaG9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="375" height="667" alt="" /></p><p></p><p>代码如下:</p><p></p><pre name="code" class="objc">//这是.h文件
//  Copyright (c) 2015年 yanhong. All rights reserved.
//

#import <Foundation/Foundation.h>
/*
    自定义的分享类,使用的是类方法,其他地方只要 构造分享内容publishContent就行了
 */
@interface ShareCustom : NSObject

+(void)shareWithContent:(id)publishContent;//自定义分享界面

@end
 

<pre name="code" class="objc">//这是.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分享类型, 
        链接地址: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<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




//为确保快速移植,这里再给大家贴一段构建分享内容的代码吧,str1和urlString怎么来的应该不用我贴了吧?如果这还需要贴的话,你还是回家去你老师办公室喝茶吧
//构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:str1
                                       defaultContent:str1
                                                image:nil
                                                title:@" "
                                                  url:urlString
                                          description:str1
                                            mediaType:SSPublishContentMediaTypeText];



shareSDK自定义分享界面UI

标签:sharesdk   自定义分享界面   ios   分享   

原文地址:http://blog.csdn.net/ehome_hong/article/details/46049397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!