IOS uses uigesturerecognizer to scale, move, rotate, and perform other operations on the image.

Source: Internet
Author: User

Uigesturerecognizer class

This class has a series of subclasses, each of which is used to identify a specific type of gesture. They are:

  • Uitapgesturerecognizer-Click the gesture. It can be configured as "click" or "combo" recognition.
  • Uipinchgesturerecognizer-"Kneading" gesture. This gesture is usually used to scale a view or change the size of a visual component.
  • Uipangesturerecognizer-"Pan" gesture. Recognize drag and drop or move actions.
  • Uiswipegesturerecognizer-"Sweep" gesture. This gesture is recognized when the user draws out of the screen. You can specify the action direction (top, bottom, left, right ).
  • Uirotationgesturerecognizer-"Rotate" gesture. The user performs a relatively circular motion on the screen.
  • Uilongpressgesturerecognizer-Long-pressed gesture. Use touch or multiple fingers to touch the screen and keep the screen for a certain period of time.

These gesture identifiers must be associated with the view through addgesturerecognizer: method. The identifier must specify a response method for calling when a specified gesture occurs. Removegesturerecognizer: The method can remove the Identifier from the view. The method parameter specifies the identifier to be removed.

The following uses an instance program to describe these gestures, add a uiimageview control to a view, and add an image. Operations on images are performed based on this view. Use these gestures for the image.

1. First, add an imageview control to a view to add an image.

    self.productImageView.image = [UIImage imageNamed:@"iPhone.jpg"];

2. Tap gesture (click)

Note: In a click, view restoration is implemented. In a double-click operation, view enlargement/reduction is doubled.

// Click uitapgesturerecognizer * singletapgesture = [[uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (resetimage :)]; singletapgesture. numberoftapsrequired = 1; // Number of tap Times [self. view topology: singletapgesture]; // double-click topology * doubletapgesture; doubletapgesture = [[initalloc] initwithtarget: Self action: @ selector (handledoubletapfrom)]; doubletapgesture. numberoftapsrequired = 2; [self. view addgesturerecognizer: doubletapgesture]; // The key is in this line. If you double-click "Confirm failed", the [singletapgesture requiregesturerecognizertofail: doubletapgesture] is triggered.

Response Method

// Click Restore view-(void) resetimage :( uitapgesturerecognizer *) recognizer {[uiview beginanimations: Nil context: Nil]; [uiview setanimationduration: 0.3]; // constant transformation. the operation between beginanimation and commitanimation is the animation process self. productimageview. transform = cgaffinetransformidentity; [self. productimageview setcenter: cgpointmake (self. view. frame. size. height/2, self. view. frame. size. width/2)]; [uiview commitanimations];} // double-click to zoom in or out.-(void) handledoubletapfrom {If (flag = Yes) {[uiview beginanimations: nil context: Nil]; [uiview setanimationduration: 0.3]; [self. productimageview setframe: cgrectmake (self. productimageview. frame. origin. x-self. productimageview. frame. size. width/2, self. productimageview. frame. origin. y-self. productimageview. frame. size. height/2, 2 * self. productimageview. frame. size. width, 2 * self. productimageview. frame. size. height)]; [uiview commitanimations]; flag = no;} else {[uiview beginanimations: Nil context: Nil]; [uiview setanimationduration: 0.3]; [self. productimageview setframe: cgrectmake (self. productimageview. frame. origin. X + self. productimageview. frame. size. width/4, self. productimageview. frame. origin. Y + self. productimageview. frame. size. height/4, self. productimageview. frame. size. width/2, self. productimageview. frame. size. height/2)]; [uiview commitanimations]; flag = yes ;}}

Note:

[SingleTapGesture requireGestureRecognizerToFail:doubleTapGesture];

Some gestures are actually associated, for example, tap and longpress, swipe and pan, or tap and Tap twice. When a uiview is added with two associated gestures at the same time, what I want to do with this finger is
Tap or longpress? According to the predefined method, if conditions are met first, the corresponding method will be jumped out and called. For example, if both pan and swipe are registered, as long as the finger moves, it will trigger the pan and then jump out, so the swipe will never happen. The single point is the same as the double point. It will always only trigger the single point, but not the double point.

Are you sure you want to solve this problem? The answer is yes. uigesturerecognizer has a method called requiregesturerecognizertofail, which can specify
The recognizer will not be triggered immediately even if it has already met the condition. It will not be triggered until the specified recognizer is determined to fail.

2. Pinch gesture (pinch and zoom)

Note: In the simulator, press Alt + the left mouse button to display the dual-finger touch screen.

// Pinch and zoom into * pinchgesture = [[financialloc] initwithtarget: Self action: @ selector (pinchview :)]; [self. View addgesturerecognizer: pinchgesture];

Response Method (two scaling response methods are provided below)

// Handle the pinch zoom gesture-(void) pinchview :( uipinchgesturerecognizer *) pinchgesturerecognizer {uiview * view = self. productimageview; If (pinchgesturerecognizer. state = uigesturerecognizerstatebegan | pinchgesturerecognizer. state = uigesturerecognizerstatechanged) {view. transform = cgaffinetransformscale (view. transform, pinchgesturerecognizer. scale, pinchgesturerecognizer. scale); pinchgesturerecognizer. scale = 1 ;}}

-(Void) scaleimage :( uipinchgesturerecognizer *) recognizer {If ([recognizer State] = success) {// If the pinch gesture ends, reset previusscale to 1.0self.previusscale = 1.0; return ;} cgfloat newscale = [recognizer scale]-self. previusscale + 1.0; cgaffinetransform currenttransformation = self. productimageview. transform; // cgaffinetransformscale (currenttransformation, 1, 1) convert to keep the original size cgaffinetransform newtransform = convert (currenttransformation, newscale, newscale); // perform the new transform self. productimageview. transform = newtransform; self. previusscale = [recognizer scale];}

Iii. Pan gesture (Translation)

// Pan Export * pangesture = [[Export alloc] initwithtarget: Self action: @ selector (panview :)]; [pangesture setminimumnumberoftouches: 1]; [pangesture restart: 1]; [self. view addgesturerecognizer: pangesture];

Response Method

// Handle the drag gesture-(void) panview :( uipangesturerecognizer *) pangesturerecognizer {uiview * view = self. productimageview; If (pangesturerecognizer. state = uigesturerecognizerstatebegan | pangesturerecognizer. state = uigesturerecognizerstatechanged) {cgpoint translation = [pangesturerecognizer translationinview: View. superview]; [view setcenter :( cgpoint) {view. center. X + translation. x, view. center. Y + translation. y}]; [pangesturerecognizer settranslation: cgpointzero inview: View. superview] ;}}

Iv. Rotate gesture (rotation)

// Rotate * rotationgesture = [[uirotationgesturerecognizer alloc] initwithtarget: Self action: @ selector (rotateview :)]; [self. View addgesturerecognizer: rotationgesture];

Response Method

// Process the rotation gesture-(void) rotateview :( uirotationgesturerecognizer *) rotationgesturerecognizer {uiview * view = self. productimageview; If (rotationgesturerecognizer. state = uigesturerecognizerstatebegan | rotationgesturerecognizer. state = uigesturerecognizerstatechanged) {view. transform = cgaffinetransformrotate (view. transform, rotationgesturerecognizer. rotation); [rotationgesturerecognizer setrotation: 0];}

The last thing to handle is the horizontal placement of the simulator.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft));}

Related Article

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.