还是直接上代码,有什么问题的话,直接评论。

  1.在YYTScratchView.h文件中

//

//  YYTScratchView.h

//  Demo-刮奖

//

//  Created by yyt on 16/4/20.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface YYTScratchView : UIView

@property (nonatomic,assign) float sizeBrush;

-(void)setHideView:(UIView*) hideView;

@end

  2在YYTScratchView.m文件中

//

//  YYTScratchView.m

//  Demo-刮奖

//

//  Created by yyt on 16/4/20.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "YYTScratchView.h"

@interface YYTScratchView (){

CGContextRef _contextMask;//maskContext 用户touch 改变的context

CGImageRef _scratchCGImg;//CGimageRef 封装_contextMask 图片信息 _contextMask改变 跟着改变 直到 调用生成UIImage

CGPoint currPonit;

CGPoint prePoint;

}

@end

@implementation YYTScratchView

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

[self setOpaque:NO];

//设置透明 如果不透明就没法看到下一层了

self.sizeBrush=10.0f;

}

return self;

}

- (void)drawRect:(CGRect)rect

{

[super drawRect:rect];

UIImage* imageToDraw=[UIImage imageWithCGImage:_scratchCGImg];

[imageToDraw drawInRect:self.frame];

}

//setSizeBrush before setHideView

-(void)setHideView:(UIView*) hideView{

CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceGray();

CGFloat scale = [UIScreen mainScreen].scale;

//获得当前传入View的CGImage

UIGraphicsBeginImageContextWithOptions(hideView.bounds.size, NO, 0);

hideView.layer.contentsScale=scale;

[hideView.layer renderInContext:UIGraphicsGetCurrentContext()];

CGImageRef hideCGImg=UIGraphicsGetImageFromCurrentImageContext().CGImage;

UIGraphicsEndImageContext();

//绘制Bitmap掩码

size_t width=CGImageGetWidth(hideCGImg);

size_t height=CGImageGetHeight(hideCGImg);

CFMutableDataRef pixels;

pixels=CFDataCreateMutable(NULL, width*height);

//创建一个可变的dataRef 用于bitmap存储记录

_contextMask = CGBitmapContextCreate(CFDataGetMutableBytePtr(pixels), width, height , 8, width, colorSpace, kCGImageAlphaNone);

//数据提供者

CGDataProviderRef dataProvider=CGDataProviderCreateWithCFData(pixels);

//填充黑色背景 mask中黑色范围为显示内容 白色为不显示

CGContextSetFillColorWithColor(_contextMask, [UIColor blackColor].CGColor);

CGContextFillRect(_contextMask, self.frame);

CGContextSetStrokeColorWithColor(_contextMask, [UIColor whiteColor].CGColor);

CGContextSetLineWidth(_contextMask, self.sizeBrush);

CGContextSetLineCap(_contextMask, kCGLineCapRound);

CGImageRef mask=CGImageMaskCreate(width, height, 8, 8, width, dataProvider, nil, NO);

_scratchCGImg=CGImageCreateWithMask(hideCGImg, mask);

CGImageRelease(mask);

CGColorSpaceRelease(colorSpace);

}

-(void)scratchViewFrom:(CGPoint)startPoint toEnd:(CGPoint)endPoint{

float scale=[UIScreen mainScreen].scale;

//CG的Y与UI的是反的 UI的y0在左上角 CG在左下

CGContextMoveToPoint(_contextMask, startPoint.x*scale, (self.frame.size.height-startPoint.y)*scale);

CGContextAddLineToPoint(_contextMask, endPoint.x*scale,(self.frame.size.height-endPoint.y)*scale);

CGContextStrokePath(_contextMask);

[self setNeedsDisplay];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[super touchesBegan:touches withEvent:event];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

[super touchesMoved:touches withEvent:event];

UITouch *touch=[touches anyObject];

currPonit=[touch locationInView:self];

prePoint=[touch previousLocationInView:self];

[self scratchViewFrom:prePoint toEnd:currPonit];

}

-(void)toucheseEnd:(NSSet *)touches withEvent:(UIEvent *)event{

[super touchesEnded:touches withEvent:event];

UITouch *touch=[touches anyObject];

currPonit=[touch locationInView:self];

prePoint=[touch previousLocationInView:self];

[self scratchViewFrom:prePoint toEnd:currPonit];

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

[super touchesCancelled:touches withEvent:event];

}

@end

  3.在需要调用的地方

//

//  ViewController.m

//  Demo-刮奖

//

//  Created by yyt on 16/4/20.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "ViewController.h"

#import "YYTScratchView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

imageView.image = [UIImage imageNamed:@"11.png"];

[self.view addSubview:imageView];

UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

imageView2.image = [UIImage imageNamed:@"22.png"];

//[self.view addSubview:imageView];

YYTScratchView *scratchView = [[YYTScratchView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

scratchView.sizeBrush = 20.0;

[scratchView setHideView:imageView2];

[self.view addSubview:scratchView];

}

@end

iOS开发——刮奖的更多相关文章

  1. iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)

    图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下 ...

  2. iOS开发--开源库

    图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩 ...

  3. iOS开发-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...

  4. iOS开发人员程序许可协议

    请细致阅读以下的许可协议条款和条件之前下载或使用苹果软件.   这些条款和条件构成你和苹果之间的法律协议.   iOS开发人员程序许可协议   目的 你想使用苹果软件(例如以下定义)来开发一个或多个应 ...

  5. 简单入门canvas - 通过刮奖效果来学习

    一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ...

  6. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  7. iOS开发系列--数据存取

    概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...

  8. iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  9. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

随机推荐

  1. 使用devcon禁用启用网卡

    系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...

  2. android 代码动态创建视图

    LinearLayout 如何动态设置 margin? LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayou ...

  3. android下m、mm、mmm编译命令的使用

    android下m.mm.mmm编译命令的使用 通过查看android源码目录下的build/envsetup.sh文件,可知: - m:       Makes from the top of th ...

  4. hadoop中联结不同来源数据

    装载自http://www.cnblogs.com/dandingyy/archive/2013/03/01/2938462.html 有时可能需要对来自不同源的数据进行综合分析: 如下例子: 有Cu ...

  5. UVALive 2053 Puzzlestan(深搜+技巧)

    这个题目的深搜形式,我也找出来了,dfs(i,j)表示第i个人选到了第j个物品,但是我却无限RE了,原因是我的viod型深搜太过暴力,我当时定义了一个计数器,来记录并限制递归的层数,发现它已经递归到了 ...

  6. ubuntu服务器移植步骤

    1.安装LAMP套件 1 tasksel 2.安装FTP工具 http://www.cnblogs.com/esin/p/3483646.html 3.安装PHPMyAdmin 1)安装 1 apt- ...

  7. L2,breakfast or lunch

    express: what a day多么糟糕的天气 I‘m coming to see you我将要来看你 what a lot of trouble he is causing他犯了多少错误啊 w ...

  8. zf-关于调用页面提示找不到className的原因

    多亏了蒋杰 还好他上次告诉我 关于节点的问题 我一看到这个函数就想到了他以前教我的    我这里一开始就调用js函数了 所以没获取到节点    后来把方法换到这里就OK了    

  9. MyEclipse9,MyEclipse10 安装ADT

    Eclipse安装ADT 时步骤是开 Eclipse IDE,进入菜单中的 "Help" -> "Install New Software" ,点击Add ...

  10. 我的android学习脚步----------- 的第一个应用

    刚刚开始学android开发,以前都是在别人调好的应用中修改JNI,现在需要自己一步步走 开发环境:Eclipse+ADT 配置不多讲了,引自:http://www.cnblogs.com/allen ...