普通版本完成的锯齿很严重 但是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

  • 大小: 566.6 KB

[ios]ios-Demo4脱衣服/刮奖app-专业的更多相关文章

  1. iOS 制作发布证书,发布到App Store

    ---恢复内容开始--- 1.登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 2.在 iOS Provisioning Portal中,点击App IDs ...

  2. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

  3. iOS利用Application Loader打包提交到App Store时遇到错误The filename 未命名.ipa in the package contains an invalid character(s). The valid characters are:A-Z ,a-z,0-9,dash,period,underscore,but the name cannot start w

    iOS利用Application Loader打包提交到App Store时遇到错误: The filename 未命名.ipa in the package contains an invalid ...

  4. iOS自定义弹出视图、收音机APP、图片涂鸦、加载刷新、文件缓存等源码

    iOS精选源码 一款优秀的 聆听夜空FM 源码 zhPopupController 简单快捷弹出自定义视图 WHStoryMaker搭建美图(贴纸,涂鸦,文字,滤镜) iOS cell高度自适应 有加 ...

  5. iOS remote debug & Android remote debug & Chrome & APP

    iOS remote debug & Android remote debug & Chrome & APP iOS remote debugging 如何在 iOS 真机上调 ...

  6. IOS小组件(8):App与Widget数据共享

    引言   Widget是一个迷你版的App,IOS有沙盒机制,不同App之间无法直接共享数据.组件和主App之间其实就是不同App的关系,所以也无法通过userdefaults.standard来传数 ...

  7. Android项目刮刮奖详解扩展篇——开源刮刮奖View的制作

    Android项目刮刮奖详解(四) 前言 我们已经成功实现了刮刮奖的功能了,本期是扩展篇,我们把这个View直接定义成开源控件,发布到JitPack上,以后有需要也可以直接使用,关于自定义控件的知识, ...

  8. Android项目刮刮奖详解(三)

    Android项目刮刮奖详解(二) 前言 上一期我们已经实现了一个简易的刮刮卡功能,这一期我们来将其完善一下 目标 将刮刮奖的宽高改为合适高度 将刮刮奖位置居中 将信息层的图片换成文字(重点) 实现 ...

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

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

随机推荐

  1. Android 操作系统的内存回收机制

    参考 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-mmry-rcycl/index.html

  2. 智能指针(二):shared_ptr实现原理

    前面讲到auto_ptr有个很大的缺陷就是所有权的转移,就是一个对象的内存块只能被一个智能指针对象所拥有.但我们有些时候希望共用那个内存块.于是C++ 11标准中有了shared_ptr这样的智能指针 ...

  3. dell ipmi sol

    http://blog.arnoudvermeer.nl/post/52375062605/howto-setup-ipmi-sol-on-a-dell-r-series-server http:// ...

  4. css选择器分类

    css选择器大致可以分为10大类: 1.元素选择器 如:p{} 2.类选择器 如:.xx{} 3.ID选择器 如:#xx{} 4.关联选择器 如:p a{} 5.子元素选择器 如:p>a{} 6 ...

  5. markdown语法学习效果预览

    注: 结合markdown官方文档 其中大部分例子和说明文字都摘自官方文档 官方链接:Markdown: Basics (快速入门). 一 段落.标题.区块代码 Markdown 支持两种标题的语法, ...

  6. 利用JSONP进行水坑攻击

    0x00 简介 前几天安全研究者Jaime Blasco发现了在中国某些特定主题的网站被进行了水坑攻击,攻击方法有一定多样性,其中存在一些比较少见于此类型攻击中的技术,不过其实是比较早的技术了,国内猥 ...

  7. CSS让div背景透明

    div背景透明样式: 样式代码: .alert{filter:alpha(opacity=100); /* IE */ -moz-opacity:1.0; /* Moz + FF */ opacity ...

  8. 登陆peoplesoft的时候显示信息

    Signon Event Message Select selectPeopleTools, then selectUtilities, then selectAdministration, then ...

  9. EasyUI datagrid checkbox数据设定与取值(转自http://blog.csdn.net/baronyang/article/dnetails/9323463,感谢分享,谢谢)

    这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数据列的 Checkbox 设定为 Che ...

  10. C#关于值类型和引用类型的备忘

      值类型 引用类型 内存分配地点 分配在栈中 分配在堆中 效率 效率高,不需要地址转换 效率低,需要进行地址转换 内存回收 使用完后,立即回收 使用完后,不是立即回收,等待GC回收 赋值操作 进行复 ...