iOS开发——刮奖

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

  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

时间: 2024-12-29 11:14:20

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

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

iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角) 2015-04-05 15:25 2482人阅读 评论(1) 收藏 举报开源框架 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作.      下载:https://github.com/mwaterfall/MWPhotoBrowser目前比较活跃的社区仍旧是Github,

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

iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下载:https://github.com/mwaterfall/MWPhotoBrowser 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在Google Code.SourceForge等地方.由于Github社区太过主流,这里主要介绍一下G

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

图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下载:https://github.com/mwaterfall/MWPhotoBrowser 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在Google Code.SourceForge等地方.由于Github社区太过主流,这里主要介绍一下Github里面流行的iOS库. 首先

iOS开发-常用第三方开源框架

特别说明:有些链接是没有用的,如需下载请在github中搜索即可. 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下载:https://github.com/mwaterfall/MWPhotoBrowser 目前比较活跃的社区仍旧是Github,除此以外也有一些不错的库散落在Google Code.SourceForge等地方.由于Github

iOS开发系列--数据存取

概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列—Objective-C之Foundation框架的文章中提到归档.plist文件存储,包括偏好设置其本质都是存储为文件,只是说归档或者plist文件存储可以选择保存到沙盒中,而偏好设置系统已经规定只能保存到沙盒的Library/Preferences目录.当然,文件存储并不作为本文的重点内容.本文重点还是说数据库存储,做过数据库开发的朋友应该知道,可以通过SQL直接访问数据库,也可以

iOS开发——高级UI&amp;带你玩转UITableView

带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实际开发中用起来却没有那么简单就是因为他结合MVC使用,涉及到了模型数据的读取,自定义View,功能的拓展和更好的解藕,下面就带你玩一遍: UITableView的两种样式 UITableViewStylePlain UITableViewStyleGroupeds accessoryType UIT

iOS开发之--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook功能开发汇总

前言 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 目录 系统应用 系统服务 2.1. 短信与邮件 2.2. 通讯录 2.3. 蓝牙 2.4. 社交 2.5. Game Center 2.6. 应用内购买 2.7. iCloud 2.8. Passbook 1. 系统应用 在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览

iOS开发系列——内购、GameCenter、iCloud、Passbook系统服务开发汇总

社交 Social 现在很多应用都内置“社交分享”功能,可以将看到的新闻.博客.广告等内容分享到微博.微信.QQ.空间等,其实从iOS6.0开始苹果官方就内置了Social.framework专门来实现社交分享功能,利用这个框架开发者只需要几句代码就可以实现内容分享.下面就以一个分享到新浪微博的功能为例来演示Social框架的应用,整个过程分为:创建内容编辑控制器,设置分享内容(文本内容.图片.超链接等),设置发送(或取消)后的回调事件,展示控制器. 程序代码: // // ViewContro

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

--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 调用系统应用 使用系统服务 短信与邮件 通讯录 蓝牙 社交 Game Center 应用内购买 iCloud Passbook 系统应用 在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览器应用,此时你可以直接使用UIApplication的OpenUR