Use gestures to scale, rotate, and move Uiimageview

Source: Internet
Author: User

[CPP]View PlainCopy
  1. Add all the gestures
  2. -(void) Addgesturerecognizertoview: (UIView *) View
  3. {
  4. //Rotation gestures
  5. Uirotationgesturerecognizer *rotationgesturerecognizer = [[Uirotationgesturerecognizer alloc] InitWithTarget:self Action: @selector (Rotateview:)];
  6. [View Addgesturerecognizer:rotationgesturerecognizer];
  7. //Zoom gesture
  8. Uipinchgesturerecognizer *pinchgesturerecognizer = [[Uipinchgesturerecognizer alloc] initwithtarget:self action:@ Selector (Pinchview:)];
  9. [View Addgesturerecognizer:pinchgesturerecognizer];
  10. //Mobile gestures
  11. Uipangesturerecognizer *pangesturerecognizer = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Panview:)];
  12. [View Addgesturerecognizer:pangesturerecognizer];
  13. }
  14. Handling Rotation gestures
  15. -(void) Rotateview: (Uirotationgesturerecognizer *) Rotationgesturerecognizer
  16. {
  17. UIView *view = Rotationgesturerecognizer.view;
  18. if (rotationgesturerecognizer.state = = Uigesturerecognizerstatebegan | | rotationgesturerecognizer.state = = uigesturerecognizerstatechanged) {
  19. View.transform = Cgaffinetransformrotate (View.transform, rotationgesturerecognizer.rotation);
  20. [Rotationgesturerecognizer setrotation:0];
  21. }
  22. }
  23. Handling Zoom gestures
  24. -(void) Pinchview: (Uipinchgesturerecognizer *) Pinchgesturerecognizer
  25. {
  26. UIView *view = Pinchgesturerecognizer.view;
  27. if (pinchgesturerecognizer.state = = Uigesturerecognizerstatebegan | | pinchgesturerecognizer.state = = uigesturerecognizerstatechanged) {
  28. View.transform = Cgaffinetransformscale (View.transform, Pinchgesturerecognizer.scale, PinchGestureRecognizer.scale );
  29. Pinchgesturerecognizer.scale = 1;
  30. }
  31. }
  32. Handling Drag gestures
  33. -(void) Panview: (Uipangesturerecognizer *) Pangesturerecognizer
  34. {
  35. UIView *view = Pangesturerecognizer.view;
  36. if (pangesturerecognizer.state = = Uigesturerecognizerstatebegan | | pangesturerecognizer.state = = uigesturerecognizerstatechanged) {
  37. Cgpoint translation = [Pangesturerecognizer TranslationInView:view.superview];
  38. [View SetCenter: (Cgpoint) {view.center.x + translation.x, View.center.y + translation.y}];
  39. [Pangesturerecognizer Settranslation:cgpointzero InView:view.superview];
  40. }
  41. }


This only requires a simple call

[CPP]View PlainCopy
    1. [Self addgesturerecognizertoview:view];
    2. If you're dealing with a picture, don't forget.
    3. [ImageView Setuserinteractionenabled:yes];
    4. [ImageView Setmultipletouchenabled:yes];


Done.

Specific use:

Define variables inside the. h file

[CPP]View PlainCopy
    1. @interface yourviewcontroller:uiviewcontroller<uigesturerecognizerdelegate>
    2. {
    3. CGFloat Lastscale;
    4. CGRect Oldframe; //Save the original size of the picture
    5. CGRect Largeframe; //Determine the maximum extent of the image magnification
    6. }

and add it to the viewdidload.

[CPP]View PlainCopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. Showimgview = [[Uiimageview alloc] Initwithframe:<span class="S1" >CGRectMake</span> (<span  class="S2" >0</span>, <span class="S2" >0</SPAN>, 320, 480)];
  5. [Showimgview Setmultipletouchenabled:yes];
  6. [Showimgview Setuserinteractionenabled:yes];
  7. [Showimgview setimage:[uiimage imagenamed:@"1.jpg"];
  8. Oldframe = Showimgview.frame;
  9. Largeframe = CGRectMake (0-screensize.width, 0-screensize.height, 3 * oldFrame.size.width, 3 * oldFrame.size.height);
  10. [Self addgesturerecognizertoview:showimgview];
  11. [Self.view Addsubview:showimgview];


This will enable the

However, this is not enough.

Because the inside of the scale and movement, etc. did not make a corresponding judgment.

Because the code is simple, the extension is also very convenient.

Modified the scaling code, added the restrictions, and other similar

[CPP]View PlainCopy
  1. Handling Zoom gestures
  2. -(void) Pinchview: (Uipinchgesturerecognizer *) Pinchgesturerecognizer
  3. {
  4. UIView *view = Pinchgesturerecognizer.view;
  5. if (pinchgesturerecognizer.state = = Uigesturerecognizerstatebegan | | pinchgesturerecognizer.state = = uigesturerecognizerstatechanged) {
  6. View.transform = Cgaffinetransformscale (View.transform, Pinchgesturerecognizer.scale, PinchGestureRecognizer.scale );
  7. if (ShowImgView.frame.size.width < oldFrame.size.width) {
  8. Showimgview.frame = Oldframe;
  9. //Make the picture smaller than the original
  10. }
  11. if (ShowImgView.frame.size.width > 3 * oldFrame.size.width) {
  12. Showimgview.frame = Largeframe;
  13. }
  14. Pinchgesturerecognizer.scale = 1;
  15. }
  16. }


That's good. The maximum and minimum proportions of the picture are guaranteed.

Reference article: http://apluck.iteye.com/blog/1781607

Use gestures to scale, rotate, and move Uiimageview

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.