首先添加友盟的sdk及设置分享跳转的URL types,还有设置白名单
分享界面的设置,一般都是要求文字配图片,图片在上,文字在下,这个网上有好多的方法
我是自定义控件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;
}
//取消按钮的高亮状态
- (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;
}
然后是在视图中设置布局,使用的是9宫格,要求的效果图
.h
//点击按钮block回调
@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;
}
分享是在工具类中,避免appdelegate重代码太多,有点小强迫
/**
* 分享到各个平台
*
* @param PlatformName 平台名称,在试图控制器里配置,具体哪些平台参考UMSocialSnsPlatformManager.h
* @param text 要分享的文本信息
* @param description 要分享的描述信息
* @param image 分享的图片信息(可以传NSData或UImage)
* @param url 要分享的网址
* @param viewController 从哪个视图控制器传过来的(一般直接写self)
*/
+ (void)shareToSocialPlatform:(id)PlatformName WithTitle:(NSString *)text andDescription:(NSString *)description andImage:(id)image andUrl:(NSString *)url showOn:(UIViewController *)viewController;
在.m中实现
+ (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) {
}
}];
}
}
都具备了,直接在控制器中实现分享,由于项目需要实现语言国际化,所以titleArr使用的是语言包,有些键值不是分享的所以需要另外做判断
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", @"复制链接.png", @"追加悬赏.png", @"添加备注.png", @"省掉见面.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];
// 下面的方法是根据tag值来做相应的事情,因为视图中除了有分享还有别的功能逻辑,所以在下面判断另行处理了
- (void)shareWithTitle:(NSInteger)btnTag
{
网友评论