自定义第三方分享弹出框
自定义第三方分享弹出框
萍水相逢OSC 发表于10个月前
自定义第三方分享弹出框
  • 发表于 10个月前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 本文主要记录自定义的分享弹出框界面及集成多个第三方分享平台

1、首先,可用nib创建一个view(如图):



2、各个分享按钮添加点击事件

//代码中的curViewController
 UIViewController* curViewController = [self topViewControllerForViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
//分段
case 1: //weibo
        {
            [[UMSocialData defaultData].extConfig.sinaData.urlResource setResourceType:UMSocialUrlResourceTypeDefault url:self.href];
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToSina]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            
            break;
        }
        case 2: //Wechat Timeline
        {
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = self.href;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.href;
            [UMSocialData defaultData].extConfig.title = self.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                NSLog(@"%u",response.responseCode);
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            break;
        }
        case 3: //WechatSession
        {
            [UMSocialData defaultData].extConfig.wechatSessionData.url = self.href;
            [UMSocialData defaultData].extConfig.wechatSessionData.title = self.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
																}
                                                            }];
            
            break;
        }
        case 4: //qq
        {
            [UMSocialData defaultData].extConfig.qqData.title = self.title;
            [UMSocialData defaultData].extConfig.qqData.url = self.href;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            
            break;
        }
        case 5: //brower
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.href]];
            
            break;
        }
        case 6: //copy url
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = [NSString stringWithFormat:@"%@", self.href];
            MBProgressHUD *HUD = [Utils createHUD];
            HUD.mode = MBProgressHUDModeCustomView;
            HUD.label.text = @"已复制到剪切板";
            if (self.superview) {
                [self removeFromSuperview];
            }
            [HUD hideAnimated:YES afterDelay:1];
            
            break;
        }
        case 7:  //more
        {
            if (self.superview) {
                [self removeFromSuperview];
            }
            
            UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSString stringWithFormat:@"分享一个很有意思的内容 %@",self.href]] applicationActivities:nil];
            if ([activityViewController respondsToSelector:@selector(popoverPresentationController)]) {
                activityViewController.popoverPresentationController.sourceView = self;
            }

            [curViewController presentViewController:activityViewController animated:YES completion:nil];
            
            break;
        }
        case 8:     //tweet
        {
            if (self.superview) {
                [self removeFromSuperview];
            }
            
            BOOL isOuterIMP = NO;
            if ([_delegate respondsToSelector:@selector(customShareModeWithShareBoard:boardIndexButton:)]) {
                isOuterIMP = [_delegate customShareModeWithShareBoard:self boardIndexButton:button.tag];
            }
            
            if (isOuterIMP) break;
            
            OSCAbout* forwardInfo = [OSCAbout forwardInfoModelWithTitle:self.authordName
                                                                          content:self.descString type:self.aboutType fullWidth:[UIScreen mainScreen].bounds.size.width - 32];
            TweetEditingVC *tweetEditingVC = [[TweetEditingVC alloc] initWithAboutID:self.aboutId aboutType:self.aboutType forwardItem:forwardInfo];
            UINavigationController *tweetEditingNav = [[UINavigationController alloc] initWithRootViewController:tweetEditingVC];
            [curViewController presentViewController:tweetEditingNav animated:YES completion:nil];
            
            break;
        }
- (UIViewController *)topViewControllerForViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController;
        return [self topViewControllerForViewController:navigationController.visibleViewController];
    }
    
    if (rootViewController.presentedViewController) {
        return [self topViewControllerForViewController:rootViewController.presentedViewController];
    }
    
    return rootViewController;
}

3、"取消"按钮隐藏弹出框:if (self.superview) {  [self removeFromSuperview]; }

4、用UITouch来控制点击背景隐藏弹出框(contentView为背景view):


#pragma mark --- touch handle 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _touchTrack = NO;
    UITouch* t = [touches anyObject];
    CGPoint p1 = [t locationInView:_contentView];
    if (!CGRectContainsPoint(_contentView.bounds, p1)) {
        _touchTrack = YES;
    }else{
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (_touchTrack) {
        if (self.superview) {
            [self removeFromSuperview];
        }
    }else{
        [super touchesEnded:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (_touchTrack) {
        if (self.superview) {
            [self removeFromSuperview];
        }
    }else{
        [super touchesCancelled:touches withEvent:event];
    }
}

5、便利构造器(当前view)
 

+ (instancetype)shareBoardWithShareType:(InformationType)infomationType
							  withModel:(id)model
{
    OSCShareBoard *curShareBoard = [[[UINib nibWithNibName:@"OSCShareBoard" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
    [curShareBoard settingShareType:infomationType model:model];
    return curShareBoard;
}

[curShareBoard settingShareType:infomationType model:model];//model有多种类型参数处理

6、设置代理

- (BOOL)customShareModeWithShareBoard:(OSCShareBoard* )shareBoard
                     boardIndexButton:(NSInteger)buttonTag;

7、在.h文件中新建NSObject类型的对象OSCShareManager,初始化,添加外调方法传数据并设置代理方法

@class OSCShareManager;
@protocol OSCShareManagerDelegate <NSObject>

@optional
- (void)shareManagerCustomShareModeWithManager:(OSCShareManager* )shareManager
                         shareBoardIndexButton:(NSInteger)buttonTag;

@end

@interface OSCShareManager : NSObject

+ (instancetype)shareManager;

- (void)showShareBoardWithShareType:(InformationType)infomationType
                          withModel:(id)model;

- (void)hiddenShareBoard;

@property (nonatomic, weak) id <OSCShareManagerDelegate> delegate;

@end

8、在.m中实现OSCShareManager方法

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SHAREBOARD_HEIGHT curShareBoard.bounds.size.height
#define SHAREBOARD_WIDTH curShareBoard.bounds.size.width

@interface OSCShareManager ()<OSCShareBoardDelegate>
{
	__weak OSCShareBoard* _curShareBoard;
}

@end

@implementation OSCShareManager

static OSCShareManager* _shareManager ;
+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareManager = [OSCShareManager new];
    });
    return _shareManager;
}

- (void)showShareBoardWithShareType:(InformationType)infomationType
						  withModel:(id)model
{
    if ([Config getOwnID] == 0) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NewLogin" bundle:nil];
        NewLoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"NewLoginViewController"];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:loginVC animated:YES completion:nil];
        return ;
    }
    
	if (_curShareBoard) { _curShareBoard = nil;}
	
	OSCShareBoard *curShareBoard = [OSCShareBoard shareBoardWithShareType:infomationType withModel:model];
	_curShareBoard = curShareBoard;
	curShareBoard.frame = [UIScreen mainScreen].bounds;
	curShareBoard = curShareBoard;
	curShareBoard.delegate = self;
	
	[[UIApplication sharedApplication].keyWindow addSubview:curShareBoard];
	
	//背景蒙层的动画:alpha值从0.0变化到0.5
	[curShareBoard.bgView setAlpha:0.0];
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
		[curShareBoard.bgView setAlpha:0.5];
	} completion:^(BOOL finished) { }];
	
	//分享面板的动画:从底部向上滚动弹出来
	[curShareBoard.contentView setFrame:CGRectMake(0, SCREEN_HEIGHT , SHAREBOARD_WIDTH, SHAREBOARD_HEIGHT )];
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
		[curShareBoard.contentView setFrame:CGRectMake(0,SCREEN_HEIGHT - SHAREBOARD_HEIGHT,SHAREBOARD_WIDTH,SHAREBOARD_HEIGHT)];
	} completion:^(BOOL finished) {}];
}

- (void)hiddenShareBoard
{
    if (_curShareBoard.superview) {
        [_curShareBoard removeFromSuperview];
    }
}

#pragma mark --- OSCShareBoardDelegate
- (BOOL)customShareModeWithShareBoard:(OSCShareBoard* )shareBoard
                     boardIndexButton:(NSInteger)buttonTag
{
    if ([_delegate respondsToSelector:@selector(shareManagerCustomShareModeWithManager:shareBoardIndexButton:)]) {
        [_delegate shareManagerCustomShareModeWithManager:self shareBoardIndexButton:buttonTag];
        return YES;
    }
    return NO;
}

@end

9、页面调用该自定义的第三方分享弹出框(activityDetail为字典类型的数据)

OSCShareManager *shareManeger = [OSCShareManager shareManager];
    [shareManeger showShareBoardWithShareType:InformationTypeActivity withModel:_activityDetail];

 

共有 人打赏支持
粉丝 104
博文 16
码字总数 4469
×
萍水相逢OSC
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: