普通版本完成的锯齿很严重
但是Ios系统中仅CGContextClearRect 并不存在cyclo等方法。
网上查了一些资料。
发现还是利用到了CG 中的Mask来实现
效果图:
这种效果可以自定义画笔的粗细等相关设置,不会像普通模式中出现比较严重的锯齿。
具体分析如下:
1.获取需要隐藏View的layer的CGImageRef用于后面配合掩码
2.开辟符合1的CGImageRef的CFMutableDataRef空间 给下面的掩码BitmapContext使用
3.通过CFMutableDataRef获得CGDataProviderRef (maskCreate用到)
3.BitmapContextCreate
4.BitmapContext填充黑色 并且设置画笔相关(白色,宽度,线帽(cap))
5.CGImageMaskCreate 将CGDataProviderRef 传入 则得到CGImageRef指向的是刚才创建BitmapContext。改变BitmapContext 则此CGImageRef内容也会改变
6.CGImageCreateWithMask - 通过Mask的CGImageRef创建CGImageRef 此CGImageRef随 Maskde CGImageRef变化(所以 BitmapContext改变 Mask改变 mask掩码后的CGImageRef改变)
7.释放
8.手指触摸时,根据上一个CGPoint 和此次的CGPoint 给bitmapContext新增path 并且绘制。然后调用【self setNeedsDisplay】,在重绘中(-drawRect) 重新获取通过mask后的CGImageRef 获取最新的UIImage
9.将最新的UIImage drawRect到界面
代码:
//
// PLLScrathView.h
// PLLScratchViewDemo
//
// Created by liu poolo on 14-7-31.
// Copyright (c) 2014年 liu poolo. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface PLLScratchView : UIView
@property (nonatomic,assign) float sizeBrush;
-(void)setHideView:(UIView*) hideView;
@end
//
// PLLScrathView.m
// PLLScratchViewDemo
//
// Created by liu poolo on 14-7-31.
// Copyright (c) 2014年 liu poolo. All rights reserved.
//
#import "PLLScratchView.h"
@interface PLLScratchView(){
CGContextRef _contextMask;//maskContext 用户touch 改变的context
CGImageRef _scratchCGImg;//CGimageRef 封装_contextMask 图片信息 _contextMask改变 跟着改变 直到 调用生成UIImage
CGPoint currPonit;
CGPoint prePoint;
}
@end
@implementation PLLScratchView
@synthesize sizeBrush;
- (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
- 浏览: 420245 次
- 性别:
- 来自: 深圳/湛江
文章分类
最新评论
-
wangyudong:
很多API doc生成工具生成doc需要重度依赖代码里加注解的 ...
[ios]利用xcode自动生成项目文档 -
tiger1819:
有源码么,想学习
[android]仿360状态,类流量监控桌面浮动显示 -
119568242:
借我一双翅膀 写道大哥,求指教啊?
IOS 开发,调用打电话,发短信,打开网址 -
借我一双翅膀:
大哥,求指教啊
IOS 开发,调用打电话,发短信,打开网址 -
li247276297:
楼主 是不是要加个权限?
[android]仿360状态,类流量监控桌面浮动显示
[ios]ios-Demo4脱衣服/刮奖app-专业
- 博客分类:
- ios
发表评论
-
[转]使用CGContextDrawImage绘制图片上下颠倒
2014-08-03 23:39 8307转自:http://www.bennychen.cn/ta ... -
[ios]ios脱衣服/刮奖app-简单版
2014-07-31 12:21 1268其实就是两张图片,然后第一张图片被手指划过的地方clear ... -
[ios]视差动画
2014-07-16 09:19 922http://www.teehanlax.com/blog/ ... -
[ios]label增加边距-既左右对齐
2014-07-09 01:23 3421http://www.cocoachina.com/bbs/ ... -
[ios] iOS 富文本类库RTLabel
2014-06-30 16:09 1594转自:http://blog.csdn.net/duxinf ... -
[ios]CA animationWIthKeyPath的一些思考
2014-05-13 19:05 2318使用CABasicAnimation的时候 总会遇到 an ... -
[ios]UIVIew的初始化执行方法
2014-04-03 14:56 1519根据你需要执行的动作, 这里有几个方法: -(id)init ... -
[ios]Gesture与scrollView分别实现图片触控
2014-04-02 21:53 1242ScrollView方法 - (void)viewDidL ... -
[ios]Gesture与scrollView分别实现图片触控
2014-04-02 21:53 1... -
[ios]block写法
2014-04-02 21:46 1034参考:http://www.cnblogs.co ... -
[ios]Block分离DataSource -ios升级日记2
2014-04-02 21:44 793根据:http://objccn.io/issue-1-1 ... -
[ios]快速打印
2014-04-02 16:04 583以前打印frame老是用fram.x.x这样 太慢。 其实 ... -
[ios]自定义圆形滑动条-ios升级日记1
2014-04-01 21:53 6597学习下自定义控件。从开源项目中挖了一个 理解模仿了一个 ... -
[ios]iOS7状态栏隐藏
2014-04-01 10:39 661在iOS7中 ,如果你还想用UIApplication ... -
OC简写
2014-03-31 19:43 1027简写 OC的语法一直被认为比较麻烦,绝大多数的 ... -
[ios]NSNotification
2014-01-09 17:48 1570这是一个观察者模式。 [[NSNotific ... -
[ios]注销键盘
2014-01-09 17:17 764[[UIApplication sharedApplica ... -
[ios]GCD-Grand Central Dispatch - 2
2014-01-02 13:04 2550Grand Central Dispatch =GDC1.有两 ... -
[ios] NSLocale 获取当前语言版本
2013-12-02 17:08 2590获取当前手机情况NSLocale 获取当前语言版本: ... -
[ios]多语言支持
2013-12-02 14:04 1126转自:http://blog.csdn.net/songro ...
相关推荐
iOS转场动画+刮奖卡
这是Android上的一个涂抹视图,用于实现刮奖效果项目地址:https://github.com/xiaopansky/RubberView效果图:...android-rubber-view-1.1.2.jarandroid-rubber-view-1.1.2-with-src.jar扫描上面的二维码可以安装demo。
刮奖效果
android刮奖效果demo,越来越流行的效果,仅作分享。
Version 1.0.0: 支持图片覆盖刮奖; 支持自定义view覆盖刮奖; 自动撤销遮罩层。 请关注我的github,不定期更新 https://github.com/PageGuo/PGScratchView
ScratchView-master刮奖
android刮奖简单实现
Android刮奖控件 功能特色: 1、可以设置刮开后显示文字或图片 2、可以统计已刮开区域所占百分比 3、可以分享刮开后的内容
Android高级应用源码-android刮奖效果.zip
自定义刮奖--电商项目
Android自定义view,实现刮奖效果,可以直接移植到工程里面直接使用。ScratchView对内存做了优化,最大程度避免OOM。不足之处就是没有做其他属性的扩展,比如自定义view的padding处理等。
向实现一些抽奖,刮奖作用,很有用的,不信你试试!
微信的刮刮乐PHP代码,有需要的可以学习参考
一个web网页,用HTML写的,css样式、JavaScript也在里面,会动,有特效,学习前端的朋友可以试着下载来看看,有些地方是可以借鉴的,挺不错的
HTML5手机端刮奖代码是一款刮刮乐手机刮奖代码。
微信刮奖活动特效
一个简单的刮奖功能
来源:Licence:Apache 2.0平台:iOS设备:... 小编注:代码利用了另外一份开源代码实现刮奖功能:/ios/Scratch-View/50da6adf6803fa6e07000000 [优才 · Code4App]编译测试,适用环境:Xcode 4.5, iOS 5.0 以上。
html5 canvas实现刮奖效果 html5 canvas实现刮奖效果 html5canvas实现刮奖效果