Touch the responder Chain
Event : An action captured by the hardware to the user
IOS of Events : Touch Time , Shaking Events , Remote Control Events
Touch Events : support multi-touch , up to one point , 6 s future devices support pressure-sensitive recognition
How do I capture a user's touch events ?
1. The user can touch all the UIView or UIView sub-classes
2. Create a UIView Subclass
3. several ways to rewrite UIView
and touch-related methods
One Touch contains : One start , one end , Multiple Moves
- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"Start Touching"); Random Positionself.center = Cgpointmake (arc4random ()% 176 +, Arc4random ()% 468 + 100); }- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"Touch Move");self.transform = cgaffinetransformrotate (Self.transform, m_pi_4);}- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"End Touch"); Change the bounds change, center unchanged, to the midpoint to expand or narrow center changes, bounds cgrect rect = self.bounds; Rect.size.width *= 1.1; Rect.size.height *= 1.1; Self.bounds = rect; Rotation Parameter 1: Original view of the deformation parameter parameter 2: Change the Radianself.transform = cgaffinetransformrotate (Self.transform, m_pi_4); }when the touch, abnormal conditions (such as to call), will block the execution of the touch, that is, call can touch cancel this method touch cancel this method more for abnormal situation- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event{NSLog (@"Cancel Touch");} @end
main.m TouchView*touchview = [[TouchView alloc] Initwithframe:cgrectmake (0,0, -, -)]; Touchview.center=Self.view.center; Touchview.backgroundcolor= [Uicolor colorwithred:0.916Green0.713Blue1.000Alpha1.000]; [Self.view Addsubview:touchview]; [TouchView release]; Dragview*dragview = [[Dragview alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Dragview.backgroundcolor=[Uicolor Cyancolor]; [Self.view Addsubview:dragview]; [Dragview release];
dragview.m#import"DragView.h"@interface Dragview () {Cgpoint startPoint;} @end @implementation Dragview- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event { touches: holds the collection of touch points, the element type of the collection Uitouch NSLog (@ "%@", touches); Uitouch: Touch class, information for holding touch points (view, location, window, clicks, click Status), inherited from NSObject Uitouch *touch = [touches anyobject]; Click Count NSLog (@ "%lu", Touch.tapcount); Timestamp (number of seconds from system boot) NSLog (@ "%.lf", Touch.timestamp); The location of the touch point on a view Cgpoint startponit = [Touch LocationInView:self.superview]; NSLog (@ "%@", Nsstringfromcgpoint (Startponit)); Touch Status: Start, move, stop, end, cancel NSLog (@ "%ld", touch.phase); Touch pressure sensation NSLog (@ "%.2LF", Touch.maximumpossibleforce);Event : events uievent, event classes, inheriting from NSObject, storing event-related information NSLog (@ "%@", event); Event Type NSLog (@ "%ld", event.type);get a touch pointUitouch *touch =[touches anyobject]; given a coordinate system, get the position of the touch pointStartPoint =[Touch LocationInView:self.superview]; NSLog (@"startPoint:%@", Nsstringfromcgpoint (StartPoint)); }- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint EndPoint=[Touch LocationInView:self.superview]; NSLog (@"endPoint:%@", Nsstringfromcgpoint (EndPoint)); //move with mouse cgpoint Point= Self.center; Point.x + = Endpoint.x-startpoint.x; Point.y + = Endpoint.y-startpoint.y; Self.center = point; StartPoint = EndPoint; }- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event { }- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event{} @end
responder chain : multiple responders
Uiresponder, Responder Class
Find out which responder was touched ( range from large to small )
1.uiapplication, application class
2.window
3.Uiviewcontroller
4.View
5. View 's child views
Note : The lookup process assembles the responder chain
Handle Touch events (range from small to large)
the first person to handle a response event , called the first responder
Rootviewcontroller.m
#import"RootViewController.h"#import"TouchView.h"#import"DragView.h"@interface Rootviewcontroller ()<UITextFieldDelegate>{Uitextfield*TextField; Uitextfield*Passwordfield;} @end @implementation Rootviewcontroller- (void) viewdidload {TextField= [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -,375- $, +)]; Textfield.borderstyle=Uitextborderstyleroundedrect; Textfield.placeholder=@"Please enter user name"; Textfield.returnkeytype=Uireturnkeynext; TextField.Delegate=Self ; [Self.view Addsubview:textfield]; [TextField release]; Passwordfield= [[Uitextfield alloc] Initwithframe:cgrectmake ( -, the,375- $, +)]; Passwordfield.borderstyle=Uitextborderstyleroundedrect; Passwordfield.placeholder=@"Please enter your password"; Passwordfield.returnkeytype=Uireturnkeydone; Passwordfield.Delegate=Self ; [Self.view Addsubview:passwordfield]; [Passwordfield release]; UIButton*button =[UIButton Buttonwithtype:uibuttontypesystem]; Button.frame= CGRectMake ( $, -,375-280, +); Button.titleLabel.font= [Uifont systemfontofsize: -]; Button.backgroundcolor= [Uicolor colorwithred:0.233Green1.000Blue0.059Alpha1.000]; [Button Settitle:@"Button"Forstate:uicontrolstatenormal]; [Button Settintcolor:[uicolor Whitecolor]; [Self.view Addsubview:button]; [Button addtarget:self Action: @selector (Press) forcontrolevents:uicontroleventtouchupinside]; let TextField be the first responder[TextField Becomefirstresponder];UIView*bgview = [[UIView alloc] Initwithframe:cgrectmake ( -, -, -, -)]; Bgview.backgroundcolor=[Uicolor Cyancolor]; userinteractionenabled, whether to turn on user interaction, if set to no, blocking the execution Bug of the responder chain: Beyond the parent view range is not clickable class with userinteractionenabled default to No 1.UILabel 2.UIImageViewBgview. userinteractionenabled =NO; [Self.view Addsubview:bgview]; [Bgview release]; Uitextfield*textfield2 = [[Uitextfield alloc] Initwithframe:cgrectmake (Ten, -, the, +)]; Textfield2.borderstyle=Uitextborderstyleroundedrect; [Bgview Addsubview:textfield2]; [TextField2 release]; }- (void) Press {NSLog (@" Press"); let TextField write off the first responder[TextField Resignfirstresponder]; [Passwordfield Resignfirstresponder];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmamark-uitextfielddelegate-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField1 {if(TextField1 = =Passwordfield) {[Passwordfield resignfirstresponder]; } Else{[Passwordfield becomefirstresponder]; } returnYES;} @end
2015-10-20 20:20:37
Touch the responder Chain