Principle:
The principle of torn clothes game is to use two almost the same photo (two photos of the difference is a more conservative, a more open, in essence, the designer in the original picture of the Open model p on a layer of clothes), the open picture is placed below, Put the conservative picture on top (each picture is placed on a imageview). The process of tearing the garment is the process of erasing the picture, erasing the place where the gesture touches the picture.
Steps:
1. Add pan gestures to the controller and set the Listening method.
2. In the Listening method
2.1 Getting the current point
2.2 Gets the erased rectangle range (centered at the current point, erasing the specified range around the current point)
2.3 Open Context
2.4 Render the layer on the control to the context
2.5 Erasing pictures
2.6. Assign the generated new image to the original ImageView
Code:
-(void) viewdidload {[Super viewdidload]; Adding pan gestures to the controller uipangesturerecognizer *pangesutre = [[Uipangesturerecognizer alloc] initwithtarget:self action: @select or (pan:)]; [Self.view addgesturerecognizer:pangesutre];} -(void) pan: (Uipangesturerecognizer *) pangesture{//1. Get current point cgpoint currentpoint = [Pangesture locationinview:self.v Iew]; 2. Get the erase rectangle range cgfloat wipewidth = 30; CGFloat wipeheight = 30; CGFloat Wipex = currentpoint.x-wipewidth * 0.5; CGFloat Wipey = currentpoint.y-wipeheight * 0.5; CGRect rect = CGRectMake (Wipex, Wipey, Wipewidth, wipeheight); 3. Open Context Uigraphicsbeginimagecontextwithoptions (self.view.frame.size, NO, 0); Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 4. Layer rendering of the control [Self.imageView.layer Renderincontext:ctx]; 5. Erase picture Cgcontextclearrect (CTX, rect); 6. Generate a new picture UIImage *image = Uigraphicsgetimagefromcurrentimagecontext (); Self.imageView.image = image;}
Effect Demo:
Picture material sharing: Tearing clothes game picture material
iphone torn clothes game-image erase