For UI objects that cannot be addtarget, adding gestures brings them the "gospel", as an example of uiview adding gestures to uncover gestures.
1, create a view first,
UIView * Jrview=[[uiviewalloc] Initwithframe:cgrectmake (00)]; Jrview.center=self.view.center; Jrview.backgroundcolor=[Uicolorgreencolor]; [Self.viewaddSubview:jrView];
2, adding gestures
Common gestures: Tap, hold, sweep (surface slip over), drag, rotate, pinch.
①: Click
First, define the click Gesture and then set the related properties of the gesture (number of clicks, Number of touchpoints ...). ), and then add the gesture to the view.
Click on the view to perform gestures @selector ( Tapgestureaction:) method (all gestures are the same);
/* * * * tap=[[Uitapgesturerecognizeralloc] initwithtarget:selfaction: @selector (tapgestureaction:)]; // -=--Click Count tap.numberoftapsrequired=2; // =-=-= number of touch points tap.numberoftouchesrequired=2; // tap.numberoftouches; ReadOnly [Jrview Addgesturerecognizer:tap];
②: Long Press
Defining gestures
Properties: A long-pressed area, a long-pressed time
Add gesture to view
/* * * * lpress=[[Uilongpressgesturerecognizeralloc] initwithtarget:selfaction: @selector (longpressaction:)]; // ===--= The recognition area of the long press (even if it is moved, long Press) lpress.allowablemovement=; // =-=-==-pressed Time-(by how long to press) lpress.minimumpressduration=0.5; [Jrview addgesturerecognizer:lpress];
③: Dragging
is so concise;
/* * * * pan=[[Uipangesturerecognizeralloc] initwithtarget:selfaction: @selector (panaction:)]; [Jrview Addgesturerecognizer:pan];
④: Swipe (surface slip over )
can set the direction of the slide, different directions to trigger different events;
/* * Swipe */ * swipe= [[uiswipegesturerecognizeralloc] initwithtarget:selfaction : @selector (swipeaction:)]; Swipe.direction=uiswipegesturerecognizerdirectionleft; [Jrview Addgesturerecognizer:swipe];
⑤: Rotate
There are agents, please check the agency agreement;
/* * Rotate */ * rotation=[[Uirotationgesturerecognizeralloc] Initwithtarget: Selfaction: @selector (rotationaction:)]; Rotation. delegate= self; [Jrview addgesturerecognizer:rotation];
⑥: Kneading
To pinch, two fingers to operate (reasonable);
/* * kneading /* pinch=[[Uipinchgesturerecognizeralloc] Initwithtarget:selfaction : @selector (pinchaction:)]; Pinch. delegate= self; [Jrview Addgesturerecognizer:pinch];
at the same time, the same view can be set to respond to multiple gestures, so there is the delegate of gestures;
#pragma The mark-proxy method, which causes the operand to respond to multiple gestures-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer{returnyes ;}
Gesture? So easy!
Add gestures to UIView