Normal version of the completed sawtooth is very serious but the iOS system only cgcontextclearrect does not exist cyclo and other methods. Some information was checked on the Internet. discover or use the mask in CG to achieve
:
This effect allows you to customize settings such as the thickness of the brush, and does not appear to be more severe jagged in normal mode.
The specific analysis is as follows:
1. Get the cgimageref of the layer that needs to hide the view for the back mate mask
2. Open Cfmutabledataref space for cgimageref of 1 to the following mask Bitmapcontext use
3. Get Cgdataproviderref through Cfmutabledataref (maskcreate)
3.BitmapContextCreate
4.BitmapContext fill black and set brush correlation (white, width, cap)
5.CGImageMaskCreate passing Cgdataproviderref The cgimageref point is the bitmapcontext you just created. Change Bitmapcontext This cgimageref content will also change
6.CGImageCreateWithMask-Create cgimageref with mask cgimageref this cgimageref changes with Maskde cgimageref (so bitmapcontext change mask change Mask Mask after the cgimageref change)
7. Release
8. Finger touch, according to the previous cgpoint and this cgpoint to bitmapcontext new path and draw. Then call "Self Setneedsdisplay" in redraw (-drawrect) to retrieve the cgimageref after mask to get the latest uiimage
9. DrawRect the latest uiimage to the interface
Code:
//
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 User Touch Changes the context
Cgimageref _scratchcgimg;//cgimageref Package _contextmask picture information _contextmask change followed by change until call generation UIImage
Cgpoint Currponit;
Cgpoint Prepoint;
}
@end
@implementation Pllscratchview
@synthesize Sizebrush;
-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
[Self setopaque:no];
Set transparency if it's opaque, you can't see the next layer.
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;
Get the cgimage of the current incoming view
Uigraphicsbeginimagecontextwithoptions (hideView.bounds.size, NO, 0);
Hideview.layer.contentsscale=scale;
[Hideview.layer Renderincontext:uigraphicsgetcurrentcontext ()];
Cgimageref Hidecgimg=uigraphicsgetimagefromcurrentimagecontext (). Cgimage;
Uigraphicsendimagecontext ();
Draw the bitmap mask
size_t width=cgimagegetwidth (HIDECGIMG);
size_t height=cgimagegetheight (HIDECGIMG);
Cfmutabledataref pixels;
Pixels=cfdatacreatemutable (NULL, width*height);
Create a mutable dataref for bitmap storage records
_contextmask = Cgbitmapcontextcreate (cfdatagetmutablebyteptr (pixels), width, height, 8, width, colorspace, Kcgimagealphanone);
Data provider
Cgdataproviderref dataprovider=cgdataprovidercreatewithcfdata (pixels);
Fill black background mask Black range for display content white to not display
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 with UI is the y0 of the UI in the top left-hand corner CG in the lower left
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
[Ios]ios-demo4 undressing/scraping app-professional