美文网首页
自定义社交化分享面板

自定义社交化分享面板

作者: Axiba | 来源:发表于2016-04-06 15:27 被阅读197次

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];
}

相关文章

  • 自定义社交化分享面板

    1、shardSDK的分享面板我们不要说他丑了,虽然它确实不好看。那么自己动手丰衣足食,那就果断自定义一个吧:(支...

  • 友盟分享

    友盟分享1、引入头文件 2、遵循协议 3、开始分享分为两种情况:一是系统自带的分享面板,二是自定义分享面板。使用友...

  • JKeyboardPanelSwitch键盘面板防抖方案

    背景 咱app社交模块,键盘和自定义面板切换的时候,出现了闪烁的现象我们使用了JKeyboardPanelSwit...

  • 1.5

    自定义面板组 打开或关闭面板 在窗口菜单中选择不同的面板名称,可以打开或关闭不同的面板,也可以打开或关闭不同的...

  • 简单美化了下沙拉查词

    沙拉查词->查词面板->自定义查词面板样式 原文链接:https://nexmoe.com/3990884387....

  • 友盟分享集成问题记录

    1、分享面板弹出后显示为空白 可能原因:SDK未初始化配置 [UMConfigure initWithAppkey...

  • 我们一起来学Photoshop之CS6的工作区(2)

    二、自定义工作区 用户也可以根据自身需要自定义工作区。例如,只在工作区显示几个需要的面板,并将面板设置在指定的区域...

  • h5调起原生分享面板,展示更多功能方案

    WebView右上角三个点按钮,点击之后会弹出分享面板,最底部展示的是h5自定义的功能,目前有两种方案展示自定义功...

  • 淘宝达人或将成为2016电商新趋势

    社交化电商是将关注、分享、沟通、讨论、互动等社交化的元素应用于电子商务交易过程的现象。顾名思义,社交化电商就是将社...

  • iOS分享---使用友盟分享(自定义分享面板)

    在新的项目中,需要实现分享功能,比较后接入友盟分享。友盟分享的使用文档以及常见问题都非常的详细,接入也较为简单。只...

网友评论

      本文标题:自定义社交化分享面板

      本文链接:https://www.haomeiwen.com/subject/mdkmlttx.html