普通版本完成的锯齿很严重 但是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. 学习记录 java随机数的产生机制

    java 随机数 一.在j2se里我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他就是个100 ...

  2. 使用ngin的静态文件下载

    1,主配置文件nginx.xml #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error. ...

  3. SQLServer转MYSQL的方法(连数据)

    本次转换需要依赖使用工具Navicat Premium. 首先,将数据库移至本地SQLServer,我试过直接在局域网上其他SQLServer服务器上想转到本地Mysql好像有问题,想将远程数据库备份 ...

  4. 必须会的SQL语句(四)删除和更新

    1.删除   1)删除记录   Delete from 表名 where id ='xx'   2)删除所有数据,并回归初始化标识字段.   Truncate table 表名   3)delete与 ...

  5. Summarize code for the three presentation experiments

    Image Picker Controller @IBAction func experiment() { let controller = UIImagePickerController() sel ...

  6. 01-实现图片按钮的缩放、动画效果(block的初步应用)

    #import "ViewController.h" #define kDelta 60 @interface ViewController () @end @implementa ...

  7. Mysql连接测试代码

    <?php $link=mysql_connect('localhost','root','htuidc'); if($link) echo "success"; ?>

  8. Objective-C特点

    1.兼容性 OC是一种面向对象的C语言,在OC的代码中可以有C和C++语句,它可以调用C函数,也可以通过C++对象访问方法. 2.字符串 OC通常不使用C语言风格的字符串.大多数情况下(CoreFou ...

  9. [leetcode]_Binary Tree Inorder Traversal

    题目:二叉树的中序遍历. 思路:用递归来写中序遍历非常简单.但是题目直接挑衅说,----->"Recursive solution is trivial".好吧.谁怕谁小狗. ...

  10. c#判断网络连接状态示例代码

    使用c#判断网络连接状态的代码. 代码: public partial class Form1 : Form { [DllImport() == true) { label1.Text = " ...