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

  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. HUD 1541/BIT(数状数组)

    题目链接 /* 按从左到右,从下到上的顺序给出星星的坐标,计算出level为[0,n)的星星的个数. 星星的level为该星星左下边(包括自己正下方的星星,但是不包括自己)星星的个数. BIT模板题. ...

  2. Loadrunner之文件的上传(八)

    老猪提供: https://mp.weixin.qq.com/s?__biz=MzIwOTMzNDEwNw==&mid=100000013&idx=1&sn=624f5bc74 ...

  3. thinkphp整合系列之rbac的升级版auth权限管理系统demo

    权限管理基本是作为网站的标配了: 除非是像博客这类个人使用的:否则权限管理的重要性不言而喻: 今个就来写写auth权限管理: thinkphp已经内置了auth权限类位于:/ThinkPHP/Libr ...

  4. Regionals 2010 :: NEERC Eastern Subregional

    遇到的问题:题目看错...(TAT英语渣渣没办法) 这里具体就讲一些思想和trick ①A题遇到了公式里面的单位问题. ②E题就是变量初始化忘记了 ③J题就是分情况讨论,实际上没有那么难...(题目读 ...

  5. Installation error: INSTALL_FAILED_UID_CHANGED 的解决办法

    出现此问题的原因大多是apk冲突造成,解决的办法如下: 1.  Settings -> Applications, 卸载出现问题的apk,重新安装即可. 2. 如果apk无法卸载,则将apk相关 ...

  6. Jquery获取input=text 的值

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. jaxb异常 Class has two properties of the same name username

    import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; im ...

  8. API 友好

    API友好 新版ThinkPHP针对API开发做了很多的优化,并且不依赖原来的API模式扩展. 数据输出 新版的控制器输出采用Response类统一处理,而不是直接在控制器中进行输出,通过设置defa ...

  9. python之路:进阶 二

        c = collections.Counter(  Counter({ b = collections.Counter(  b.update(c)   Counter({ Counter({  ...

  10. pcap filter

    今天用tshark抓包,本以为wireshark能用的filter,如“mysql”它也应该能用,其实不然:tshark -f只认识pcap filter,用-R的话,说要用-2,加上-2的话又说什么 ...