iOS issues summary: 2015-2-10 gesture usage

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size: 15px;"><span style="font-size: 15px;">1. Add a tap gesture (tapgesturerecognizer)</span></span><p><p>The code for the initialization code Tapgesturerecongnizer is as Follows:</p></p><p><p>1//new Tap gesture<br>2 UITapGestureRecognizer *tapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action: @selector ( tapgesture:)];<br>3//set number of clicks and click Hand Index<br>4 tapgesture.numberoftapsrequired = 1; Click Count<br>5 tapgesture.numberoftouchesrequired = 1; Click Hand Index<br>6 [self.view addgesturerecognizer:tapgesture];<br>Add the appropriate business logic in the callback Method:</p></p><p><p>1//tap Gesture Trigger Method<br>2-(void) tapgesture: (id) Sender<br>3 {<br>4//what to do after a light click<br><br>5}<br><strong>2. Long press gesture (longpressgesturerecognizer)</strong></p></p><p><p>Initialization code:</p></p><p><p>Add a long push gesture<br>2 Uilongpressgesturerecognizer *longpressgesture = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@ Selector (longpressgesture:)];<br>3//set long time between<br>4 Longpressgesture.minimumpressduration = 0.5; (2 Seconds)<br>5 [self.view addgesturerecognizer:longpressgesture];<br>Add the appropriate method to the corresponding callback method (executed when the gesture starts):</p></p><p><p>1//normal gesture Triggering method<br>2-(void) longpressgesture: (id) Sender<br>3 {<br>4 Uilongpressgesturerecognizer *longpress = sender;<br>5 if (longpress.state = = Uigesturerecognizerstatebegan)<br>6 {<br>7 Uialertview *alter = [[uialertview alloc] initwithtitle:@ "prompt" message:@ "long Press trigger" delegate:nil cancelbuttontitle:@ "cancel" otherbuttontitles:nil];<br>8 [alter show];<br>3 ·<br>0}<br>Code description: The common status of gestures is as follows</p></p><p><p>Start: Uigesturerecognizerstatebegan</p></p><p><p>Change: uigesturerecognizerstatechanged</p></p><p><p>Ends: uigesturerecognizerstateended</p></p><p><p>Cancel: uigesturerecognizerstatecancelled</p></p><p><p>Failure: uigesturerecognizerstatefailed</p></p><p><p><strong>3. Swipe gesture (swipegesturerecognizer)</strong></p></p><p><p>When you initialize a swipe gesture, you have to specify the direction of the swipe, up or down. If you want to add multiple swipe directions, you have to add multiple swipe gestures, but the callback is the same method.</p></p><p><p>To add a swipe gesture, one left to the right, the code is as Follows:</p></p><p><p>1//add a swipe gesture<br>2 Uiswipegesturerecognizer *swipegesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( swipegesture:)];<br>3//set the direction of the swipe<br>4 Swipegesture.direction = uiswipegesturerecognizerdirectionright; Default Right<br>5 [self.view addgesturerecognizer:swipegesture];<br>6<br><br>7//add a swipe gesture<br>8 Uiswipegesturerecognizer *swipegestureleft = [[uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)];<br>9//set the direction of the swipe<br>Ten swipegestureleft.direction = uiswipegesturerecognizerdirectionleft; Default Right<br>[self.view addgesturerecognizer:swipegestureleft];<br>The callback method is as Follows:</p></p><p><p>1//swipe Gesture Trigger Method<br>2-(void) swipegesture: (id) Sender<br>3 {<br>4 Uiswipegesturerecognizer *swipe = sender;<br>5 if (swipe.direction = = Uiswipegesturerecognizerdirectionleft)<br>6 {<br>7//left Swipe to do things<br>8}<br>9 if (swipe.direction = = Uiswipegesturerecognizerdirectionright)<br>10 {<br>11//swipe Right to do things<br>12}<br>13}<br>14<br><br><strong>4. Pinch gesture (pinchgesturerecognizer)</strong></p></p><p><p>Pinch gesture Initialization</p></p><p><p>Add a pinch gesture<br>2 Uipinchgesturerecognizer *pinchgesture = [[uipinchgesturerecognizer alloc] initwithtarget:self action: @selector ( pinchgesture:)];<br>3 [self.view addgesturerecognizer:pinchgesture];<br>How to trigger a pinch gesture (enlarge or shrink the image):</p></p><p><p>1////pinch gesture Triggering method<br>2-(void) pinchgesture: (id) Sender<br>3 {<br>4 Uipinchgesturerecognizer *gesture = sender;<br>5<br><br>6//when gesture Changes<br>7 if (gesture.state = = Uigesturerecognizerstatechanged)<br>8 {<br>9//pinch gesture The Scale property records the scaling scale<br>Ten _imageview.transform = Cgaffinetransformmakescale (gesture.scale, gesture.scale);<br>11}<br>12<br><br>13//after End Recovery<br>If (gesture.state==uigesturerecognizerstateended)<br>15 {<br>[UIView animatewithduration:0.5 animations:{<br>_imageview.transform = Cgaffinetransformidentity;//remove all deformations<br>18}];<br>19}<br>20}<br><strong>5. Drag gestures (pangesturerecognizer)</strong></p></p><p><p>Initialization of the drag gesture</p></p><p><p>Add a drag gesture<br>2 Uipangesturerecognizer *pangesture = [[uipangesturerecognizer alloc] initwithtarget:self action: @selector ( pangesture:)];<br>3 [self.view addgesturerecognizer:pangesture];<br>The way to do the drag gesture (get moving points by translationinview, similar to the touchesmoved METHOD)</p></p><p><p>1//drag gestures<br>2-(void) pangesture: (id) Sender<br>3 {<br>4 Uipangesturerecognizer *pangesture = sender;<br>5<br><br>6 Cgpoint movepoint = [pangesture translationInView:self.view];<br>7<br><br>8//do what you want to do<br>3 ·<br><strong>6. Rotation gesture (rotationgesturerecognizer)</strong></p></p><p><p>The initialization of the rotation gesture</p></p><p><p>Add a rotation gesture<br>2 Uirotationgesturerecognizer *rotationgesture = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@ Selector (rotationgesture:)];<br>3 [self.view addgesturerecognizer:rotationgesture];<br>How to invoke a rotation gesture:</p></p><p><p>Copy Code</p></p><p><p>1//rotation gesture<br>2-(void) rotationgesture: (id) Sender<br>3 {<br>4<br><br>5 Uirotationgesturerecognizer *gesture = sender;<br>6<br><br>7 If (gesture.state==uigesturerecognizerstatechanged)<br>8 {<br>9 _imageview.transform=cgaffinetransformmakerotation (gesture.rotation);<br>10}<br>11<br><br>If (gesture.state==uigesturerecognizerstateended)<br>13 {<br>14<br><br>[UIView Animatewithduration:1 animations:{<br>_imageview.transform=cgaffinetransformidentity;//cancel the deformation<br>17}];<br>18}<br>19<br><br>20}</p></p><p><p>iOS issues summary: 2015-2-10 gesture usage</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.