Recent study progress is slow, because there is a new project to go online before the end of the year, and every time the business staff come over once, the demand has changed, so constantly change to change = =! Oh ~ Don't say the heart is so tired
2015/11/29
Day 43
Generation and delivery of events
After a touch event occurs, the system adds the event to an event queue managed by UIApplication
UIApplication takes the first event from the event queue and distributes the event for processing, typically sending an event to the application's main window (Keywindow)
The main window will find the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process.
Once the appropriate view control is found, the touches method of the view control is called to do the specific event handling
Touchesbegan ...
Touchesmoved ...
Touchedended ...
UIView Three cases of not receiving touch events
Do not receive user interaction
userinteractionenabled = NO
Hide
Hidden = YES
Transparent
Alpha = 0.0 ~ 0.01
Tip: Uiimageview 's userinteractionenabled default is no, so Uiimageview and its child controls cannot receive touch events by default .
Detailed process of touch event handling
A touch event that occurs after a user taps the screen and, after passing through some columns, finds the most appropriate view control to handle the event
Once the most appropriate view control is found, the touches method of the control is called to make specific event handling
Touchesbegan ...
Touchesmoved ...
Touchedended ...
The default practice of these touches methods is to pass the event up the responder chain, handing the event to the previous responder for processing
Responder Chain
The event delivery process for the responder chain
1. If the view controller is present, it is passed to the controller, and if the controller does not exist, it is passed to its parent view
2. in the top-most view of the view hierarchy, if the received event or message cannot be processed, it passes the event or message to the window object for processing
3. If the window object is not processed, it passes the event or message to the UIApplication object
4. If uiapplication cannot process the event or message, discard it
The practice of listening for touch events
If you want to listen to a touch event on a view, the previous practice is to
Customizing a View
Implement the touches method of view, implement the specific processing code inside the method
There are a few obvious drawbacks to monitoring the view touch event through the touches method
You have to customize the view
Because the touch event is monitored in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view by default
Not easily differentiate user's specific gesture behavior
After IOS 3.2, Apple introduced the gesture recognition feature (Gesture recognizer), which greatly simplifies developer development in touch event handling
Uigesturerecognizer
In order to complete gesture recognition, the gesture recognizer must be used----Uigesturerecognizer
With Uigesturerecognizer, you can easily identify some common gestures that users make on a view
Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures and uses its subclasses to handle specific gestures
UITapGestureRecognizer ( percussion)
Uipinchgesturerecognizer ( Pinch, for zooming)
Uipangesturerecognizer ( drag)
Uiswipegesturerecognizer (swipe )
Uirotationgesturerecognizer ( swivel)
Uilongpressgesturerecognizer ( long Press)
UITapGestureRecognizer
- Each gesture recognizer uses a similar set of steps, such as UITapGestureRecognizer:
- To create a gesture recognizer object
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
- To set the specific properties of a gesture Recognizer object
2 consecutive strokes
tap.numberoftapsrequired = 2;
It takes 2 fingers to tap together .
tap.numberoftouchesrequired = 2;
- Add a gesture recognizer to the corresponding view
[Self.iconview Addgesturerecognizer:tap];
- Triggering of a listening gesture
[Tap addtarget:self Action: @selector (Tapiconview:)];
Status of gesture recognition
typedef ns_enum (Nsinteger, uigesturerecognizerstate) {//No touch event occurs, default state of all gesture recognitionuigesturerecognizerstatepossible,//when a gesture has started but has not changed or completedUigesturerecognizerstatebegan,//Gesture State Changeuigesturerecognizerstatechanged,//Gesture Completionuigesturerecognizerstateended,//gesture cancellation, revert to possible stateuigesturerecognizerstatecancelled,//gesture failed to revert to possible stateuigesturerecognizerstatefailed,//recognition to gesture recognitionuigesturerecognizerstaterecognized =uigesturerecognizerstateended};
Then use uigesturerecognizer To do a rotation, zoom, drag the view of the demo, the operation is a ImageView, the code is as follows
@interfaceViewcontroller () <UIGestureRecognizerDelegate>@property (Weak, nonatomic) Iboutlet Uiimageview*image;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //ZoomUipinchgesturerecognizer *pinch =[[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (Pinchview:)]; Pinch.Delegate=Self ; [Self.image Addgesturerecognizer:pinch]; //RotateUirotationgesturerecognizer *rotate =[[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector (Rotateview:)]; Rotate.Delegate=Self ; [Self.image Addgesturerecognizer:rotate]; //draggingUipangesturerecognizer *pan =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Panview:)]; Pan.Delegate=Self ; [Self.image Addgesturerecognizer:pan];}- (void) Pinchview: (Uipinchgesturerecognizer *) Pinch {pinch.view.transform=Cgaffinetransformscale (Pinch.view.transform, Pinch.scale, Pinch.scale); Pinch.scale=1;}- (void) Rotateview: (Uirotationgesturerecognizer *) Rotate {rotate.view.transform=cgaffinetransformrotate (Rotate.view.transform, rotate.rotation); Rotate.rotation=0;}- (void) Panview: (Uipangesturerecognizer *) pan {cgpoint translation=[Pan TranslationInView:pan.view]; Pan.view.transform=cgaffinetransformtranslate (Pan.view.transform, translation.x, TRANSLATION.Y); [Pan Settranslation:cgpointzero InView:pan.view];} #pragmaMark-Proxy method/** Make all gestures work*/-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer {returnYES;}@end
swift version of the code
Import Uikitclass Viewcontroller:uiviewcontroller, uigesturerecognizerdelegate {@IBOutlet weak var image:uiimageview ! Override Func Viewdidload () {super.viewdidload ()//drag Let pan = Uipangesturerecognizer (target:self , Action:Selector.init ("Panview:")) Pan.delegate = self Self.image.addGestureRecognizer (PAN)//rotate Let rotate = Uirotationgesturerecognizer (target:self, Action:Selector.init ("Rotateview:")) Rotate.delegate = Self Self.image.addGestureRecognizer (rotate)//zoom Let pinch = Uipinchgesturerecognizer (target:self, Action:Selector.init ("Pinchview:")) Pinch.delegate = self Self.image.addGestureRecognizer (pinch)} f UNC Gesturerecognizer (Gesturerecognizer:uigesturerecognizer, Shouldrecognizesimultaneouslywithgesturerecognizer Othergesturerecognizer:uigesturerecognizer), Bool {return true} func Panview (Pan:uipangesturerecogniz ER) {Let TranslaTe = Pan.translationinview (Pan.view) Pan.view?. Transform = Cgaffinetransformtranslate ((Pan.view?. Transform)!, translate.x, Translate.y) pan.settranslation (Cgpoint (x:0, y:0), InView:pan.view)} func Rotat Eview (Rotate:uirotationgesturerecognizer) {Rotate.view?. Transform = Cgaffinetransformrotate ((Rotate.view?. Transform)!, rotate.rotation) rotate.rotation = 0} func pinchview (Pinch:uipinchgesturerecognizer) {p Inch.view?. Transform = Cgaffinetransformscale ((Pinch.view?. Transform)!, Pinch.scale, Pinch.scale) Pinch.scale = 1}}
A Java programmer learns the path of iOS development (11)