开发技巧

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

2016-10-25  本文已影响110人  iOS学末

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

第一步

//在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.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就会弹出分享 【__一定要真机测试哦 __】

上一篇下一篇

猜你喜欢

热点阅读