iOS Development-Scratch Awards

Source: Internet
Author: User
Tags uikit

or directly on the code, if there is any problem, direct comment.

1. In the YYTScratchView.h file

//

YYTScratchView.h

Demo-Scraping Award

//

Created by Yyt on 16/4/20.

COPYRIGHT©2016 year Yyt. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface Yytscratchview:uiview

@property (nonatomic,assign) float Sizebrush;

-(void) Sethideview: (uiview*) Hideview;

@end

2 in the yytscratchview.m file

//

yytscratchview.m

Demo-Scraping Award

//

Created by Yyt on 16/4/20.

COPYRIGHT©2016 year Yyt. All rights reserved.

//

#import "YYTScratchView.h"

@interface Yytscratchview () {

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 Yytscratchview

-(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

3. Where calls are required

//

Viewcontroller.m

Demo-Scraping Award

//

Created by Yyt on 16/4/20.

COPYRIGHT©2016 year 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

iOS Development-Scratch Awards

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.