After learning the Uiview,uicontrol class, many people know the use of touchesbegain,touchesmoved,touchesend,gesturerecognizer, but careful consideration of the relationship between these events is a headache.
Now take an example to analyze their internal implementations:
-(void) viewdidload
{
UIButton * Btn=[[uibutton Alloc]initwithframe:cgrectmake (20, 40, 50, 50)];
[Self.view ADDSUBVIEW:BTN];
Btn.backgroundcolor=[uicolor Redcolor];
[BTN release];
UITapGestureRecognizer * Tap11=[[uitapgesturerecognizer alloc]initwithtarget:self Action: @selector (Tapmethod11 :)];
[btn ADDGESTURERECOGNIZER:TAP11];
[btn addtarget:self Action: @selector (Bthmethod:) forcontrolevents:uicontroleventtouchupinside];
Pay attention to the three sentences red, the following will focus on its analysis
}
-(void) Tapmethod11: (UITapGestureRecognizer *) tap
{
NSLog (@ "%@", tap);
}
-(void) Bthmethod: (UIButton *) btn
{
NSLog (@ "%@", BTN);
}
Instantiate a button in a View controller viewdidload, add two events one uicontroleventtouchupinside, another gesture event UITapGestureRecognizer,
Clicking on startup will find that the program will only perform gesture events
What's going on?
First of all to analyze UIButton This class, this class indirectly inherited from UIView, then UIButton inside must have
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
}
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
}
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
}
....
These few events
Next look at [btn addtarget:self action: @selector (Bthmethod:) forcontrolevents:uicontroleventtouchupinside]; This line of code,
This code is when BTN triggers this uicontroleventtouchupinside (mouse click event), it triggers the bthmethod inside the current controller: this method.
Then there is definitely a code for [self performSelector:bthmethod:withObject:self] in this class of UIButton .
The crux of the matter is here, where is this event triggered by UIControlEventTouchUpInside? You can do an experiment (you can try it on your own computer), when you click this button, NSLog print out the information is printed after the mouse click the key, all can infer [self performSelector:bthmethod:withObject: Self] This code is written in-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event (this is determined in the event, because the delegate method must be event-triggered)
So the touchesended code writes this first.
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
[Self performSelector:bthmethod:withObject:self];
}
The following is an analysis of gesture recognition Gesturerecognizer
Gesture recognition is inherited from NSObject and seems to have nothing to do with UIButton, but careful analysis will find:
UITapGestureRecognizer * Tap11=[[uitapgesturerecognizer alloc]initwithtarget:self Action: @selector (TAPMETHOD11:)];
The initialization of gesture recognition (for example, UITapGestureRecognizer here) is similar to the Addtarget action method
Note that in the class of gesture recognition, there is also [self performSelector:tapmethod11:withObject:self]; This code
But gesture recognition is not inherited from UIView, all it does not have a touch event
Let's just assume that there's a method1 method in his class, so the code can say
@implement UITapGestureRecognizer
-(void) method
{
[Self performSelector:tapmethod11:withObject:self];
}
@end
As you should know, this method is not an event method, so it is impossible to be called for nothing, but when we click the button it triggers, what is the reason?
Next look at [btn Addgesturerecognizer:tap11], this sentence, at this time UIButton a method
Call this method in UIButton to implement gesture recognition
It can be inferred that there is a private field in UIButton Uigesturerecognizer * gesture; (why is it private, because it is common that we can see it in its definition)
Then the above [btn ADDGESTURERECOGNIZER:TAP11]; is to give gesture assignment
After the assignment gesture calls the method, it points to the event above
The specific code is as follows:
@interface UIButton()
{
Uigesturerecognizer * gesture;
}
@end
@implementation UIButton
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
[Gesture method];
[Self performSelector:bthmethod:withObject:self];
}
@end
We've got two lines of code in touchesended now.
But the click button only executes the gesture method, and all the code has been improved as follows to get a conclusion
@implementation UIButton
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
if (Gesture!=nil)
{
[Gesture method];
}
Else
{
[Self performSelector:bthmethod:withObject:self];
}
}
@end
This explains the questions we ask at the beginning, why gestures and click events only respond to gestures
In fact, after the above reasoning, we can also explain why gesture will also have gestures to start (Uigesturerecognizerstatebegan), change (uigesturerecognizerstatechanged,), End ( uigesturerecognizerstateended, etc.) status
Because it's exactly a copy of the life cycle of the UIView event.
Then the final version of the UIButton class is:
@interface UIButton()
{
Uigesturerecognizer * gesture;
}
@end
@implementation UIButton
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
[Gesture method];
[Self performSelector:bthmethod:withObject:self];
}
@end
We've got two lines of code in touchesended now.
But the click button only executes the gesture method, and all the code has been improved as follows to get a conclusion
@implementation UIButton
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
if (Gesture!=nil)
{
gesture.state=uigesturerecognizerstateended;
[Gesture method];
}
}
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
if (Gesture!=nil)
{
gesture.state=uigesturerecognizerstatechanged;
[Gesture method];
}
}
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
if (Gesture!=nil)
{
Gesture.state=uigesturerecognizerstatebegan;
[Gesture method];
}
Else
{
[Self performSelector:bthmethod:withObject:self];
}
}
....
@end
The above is I write code inference, of course, there are various types of gesture recognition, it is impossible to just a few lines of code can be achieved.
If there is any mistake, please point out the common progress