Rootviewcontroller
#import"RootViewController.h"#import"TouchView.h"#import"PanView.h"#import"PinchView.h"@interface Rootviewcontroller () @end @implementation Rootviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view. //TouchView *greenview = [[TouchView alloc] Initwithframe:cgrectmake ($, 184, (+))];//GreenView.layer.cornerRadius = GREENVIEW.FRAME.SIZE.WIDTH/2;//Greenview.backgroundcolor = [Uicolor greencolor];// //[Self.view Addsubview:greenview];//[Greenview release];//Panview *greenview = [[Panview alloc] Initwithframe:cgrectmake ($, 184, (+))];//GreenView.layer.cornerRadius = GREENVIEW.FRAME.SIZE.WIDTH/2;//Greenview.backgroundcolor = [Uicolor greencolor];// //[Self.view Addsubview:greenview];//[Greenview release];Pinchview *redview = [[Pinchview alloc] Initwithframe:cgrectmake ( -, +,280, -)]; Redview.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:redview]; [Redview release]; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}/*#pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before n avigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [s] Egue Destinationviewcontroller]. Pass the selected object to the new view controller.}*/- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{UIView*view = self.view.subviews[0]; View.backgroundcolor=[Uicolor Redcolor];} @end
touchview.m
#import"TouchView.h"#import"uicolor+creat.h"@implementation TouchView/*why define a subclass of UIView? Because when we want UIView to respond to touch events, we need to implement several methods in its. m file, UIView cannot implement these methods in its. m file, so you need to define the subclasses of UIView If you want to respond to multiple touch events in a single view, you need to implement a few ways to respond to touch events*//** Trigger when first touch (finger just touches the view)*/- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //touches store Finger Click Object, corresponding class is Uitouch//1. Get the point of finger clickUitouch *touch = [touches anyobject];//quickly remove the clicked object from the collection//2. Get the number of clicks if(1==touch.tapcount) {//when the recognition is a click action Yes, deferred execution to see if it is a double-click Operation[Self performselector: @selector (changeselfcolor) Withobject:nil Afterdelay:0.3]; //[self changeselfcolor];}Else if(2==Touch.tapcount) { //when the recognition is double-clicked, cancel the click action[NSObject cancelpreviousperformrequestswithtarget:self selector: @selector (Changeselfcolor)Object: nil]; [Self changesupcolor]; }}//triggered when the finger moves within the view (provided that the finger has not left the view)- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"%s,%d", __function__,__line__); }//when the phone leaves the view, the touch ends.- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{//NSLog (@ "%s,%d", __function__,__line__);//[self changesupcolor];//[self changeselfframe];}//triggered when touch is canceled (triggering is accidentally interrupted, such as a telephone connection)- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"%s,%d", __function__,__line__);}//Modify the own view color- (void) changeselfcolor{Self.backgroundcolor=[Uicolor Randomecolor];}//To modify the parent view color- (void) changesupcolor{Self.superview.backgroundColor=[Uicolor Randomecolor];}//Modify your own location- (void) changeselfframe{Self.center= Cgpointmake (arc4random ()%101+ -, Arc4random ()%301+ -);}//implementation click, double- click@end
panview.m
#import"PanView.h"@implementation Panview- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{ //1. Get Finger objectsUitouch *touch =[touches anyobject]; //2. Get the current position of the fingerCgpoint Currentpoint =[Touch locationinview:self]; //3. Get the position before the fingerCgpoint Previouspoint =[Touch previouslocationinview:self]; //4. Calculate the increment of the moveCGFloat dx = currentpoint.x-Previouspoint.x; CGFloat Dy= Currentpoint.y-Previouspoint.y; //Modify View LocationSelf.center = cgpointmake (self.center.x + dx, SELF.CENTER.Y +dy);} @end
pinchview.m
#import"PinchView.h"@implementation Pinchview-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) { //IOS supports multi-touch, but the default is single touchself.multipletouchenabled =YES; } returnSelf ;}- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{ if(1==[touches Count]) { return;//If Touch view is only a little, do not pinch } //1. Get Finger objectsNsarray *alltouch =[touches allobjects]; //without the benefit of subscript, will not cause the program to crash (array subscript out of bounds)Uitouch *firsttouch =[Alltouch Firstobject]; Uitouch*secondtouch =[Alltouch Lastobject]; //2 Get two Finger object current positionCgpoint Currentfirstpoint =[Firsttouch locationinview:self]; Cgpoint Currentsecondpoint=[Secondtouch locationinview:self]; //3. Get the position of the previous fingerCgpoint Previousfirstpoint=[Firsttouch previouslocationinview:self]; Cgpoint Previoussecondpoint=[Secondtouch previouslocationinview:self]; //4. Calculate the distance of the current two fingersCGFloat currentdistance =[self distancefrompoint:currentfirstpoint topoint:currentsecondpoint]; //5. Calculate the distance of two fingers before kneadingCGFloat previousdistance =[self distancefrompoint:previousfirstpoint topoint:previoussecondpoint]; //6. Find out the proportions of the cows and the two kneadingCGFloat rate = currentdistance/previousdistance; //7. Zoom if the view size changes and you want to keep the center point intact, modify the boundsSelf.bounds = CGRectMake (0,0, Self.bounds.size.width * rate, Self.bounds.size.height *Rate ); }//calculate the distance between two points-(CGFloat) DistanceFromPoint: (cgpoint) frompoint topoint: (cgpoint) topoint{cgfloat DX= Frompoint.x-Topoint.x; CGFloat Dy= Frompoint.y-Topoint.y; returnsqrt (DX * dx + dy *dy); } @end
An explanation of four-touch events in iOS-pan-pinch-slide (touchesbegan,touchesmoved,touchesended,touchescancelled)