iOS 类似好奇心日报分享功能Demo

在iOS中,可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享。比如你可以从微信复制一个url,粘贴到浏览器中查看这个链接的内容。而好奇心日报则是以图片的形式分享。

  • 好奇心日报分享功能:复制文字利用系统分享,分享的内容不再是简单的文字,而是一张图片(类似于封装好的cell,样式固定,只是替换分享的内容),且上面有二维码等。

  • 在iOS中下面三个控件,自身就有复制-粘贴的功能:
    1、UITextView
    2、UITextField
    3、UIWebView

  • UIKit framework提供了几个类和协议方便我们在自己的应用程序中实现剪贴板的功能。
    1、UIPasteboard:我们可以向其中写入数据,也可以读取数据
    2、UIMenuController:显示一个快捷菜单,用来复制、剪贴、粘贴选择的项。

第一步

  • 创建ModelView类,继承UITextView(因为分享要分享的是图片,要写Share方法,所不能直接用UITextView)
//在ModelView.m 需要实现的方法
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
//需要什么动作 设置return为YES  
    if (action == @selector(cut:)){
        return NO;
    }
    else if(action == @selector(copy:)){
        return YES;
    }
    else if(action == @selector(paste:)){
        return NO;
    }
    else if(action == @selector(select:)){
        return YES;
    }
    else if(action == @selector(selectAll:)){
        return YES;
    }
    else if(action == @selector(_share:)){
        return YES;
    }
    else
    {
        return [super canPerformAction:action withSender:sender];
    }
}
//分享事件  imageView是分享的模板
- (void)_share:(id)sender 
{
    //获取粘贴板上分享内容
    [self CreateImageView];
   
    UIImage *image = [self makeImageWithView:_imageView];
    
    NSArray *postItems=@[image];
    //调用系统分享 把图片发送
    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
    //系统分享需要控制器弹出,利用modelView找到其所在的视图控制器
    UIViewController *viewC = [self parentController];
    [viewC presentViewController:controller animated:YES completion:nil];
}
//复制事件
- (void)copy:(id)sender {
    //复制字符串到粘贴板上
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.text;
    NSLog(@"123");
}
//分享的图片模板
- (void)CreateImageView
{
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,200,304-10,150)];
    _imageView.backgroundColor = [UIColor lightGrayColor];
    
    UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake(20,60,304-70,100)];
    text.text = self.text;
    text.backgroundColor = [UIColor lightGrayColor];
    text.sizeToFit;
    [_imageView addSubview:text];
    
    UIImageView *image = [UIImageView new];
    image.frame = CGRectMake(10, 10, 30, 30);
    image.image = [UIImage imageNamed:@"search.jpg"];
    image.layer.cornerRadius = 15;
    image.clipsToBounds = YES;
    [_imageView addSubview:image];
    
    UITextView *text2 = [[UITextView alloc]initWithFrame:CGRectMake(50,15,200,30)];
    text2.text = @"我 分 享 了 一 段 文 字";
    text2.backgroundColor = [UIColor lightGrayColor];
    text2.sizeToFit;
    [_imageView addSubview:text2];
    
}
//截取view生成image
- (UIImage *)makeImageWithView:(UIView *)view

{
    CGSize s = view.bounds.size;
    
    // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数。
    
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}

//利用View找到其所在的viewController
-(UIViewController*)parentController{
    UIResponder *responder = [self nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}

第二步

  • 创建ShareViewController类
//在ShareViewController.m 实现的方法
#import "ShareViewController.h"
#import "ModelView.h"
@interface ShareViewController (){

    UIImageView *_imageView;
}

@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTextView];
    
}
- (void)createTextView
{
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,200,404-10,150)];
    _imageView.backgroundColor = [UIColor lightGrayColor];
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    //头像
    UIImageView *image = [UIImageView new];
    image.frame = CGRectMake(10, 10, 30, 30);
    image.image = [UIImage imageNamed:@"search.jpg"];
    image.layer.cornerRadius = 15;
    image.clipsToBounds = YES;
    [_imageView addSubview:image];
    //昵称
    UITextView *text2 = [[UITextView alloc]initWithFrame:CGRectMake(45,12,200,30)];
    text2.text = @"爱上你等于爱猪";
    text2.backgroundColor = [UIColor lightGrayColor];
    text2.sizeToFit;
    [_imageView addSubview:text2];
    //想分享的内容
    ModelView *model = [[ModelView alloc]initWithFrame:CGRectMake(45, 45, 304, 100)];
    model.text = @"  在iOS中,可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享。比如你可以从iPhone QQ复制一个url,然后粘贴到safari浏览器中查看这个链接的内容。";
    model.backgroundColor = [UIColor lightGrayColor];
    model.sizeToFit;
    [_imageView addSubview:model];
}

程序运行,运行结果:

屏幕快照 2016-10-15 下午3.10.12.png

我们长按文字,会出现菜单

屏幕快照 2016-10-15 下午3.12.47.png

我们既可以选择需要想分享的文字 点击copy【此时粘贴板上就回有我们想分享的文字】,在点击Share就会弹出分享 【__一定要真机测试哦 __】

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容