"Go" uses gestures to scale, rotate, and move Uiimageview

Source: Internet
Author: User

Original Address http://blog.csdn.net/crazy_frog/article/details/8664108/

//Add all the gestures- (void) Addgesturerecognizertoview: (UIView *) view{//Rotate gesturesUirotationgesturerecognizer *rotationgesturerecognizer =[[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector (Rotateview:)];        [View Addgesturerecognizer:rotationgesturerecognizer]; //Zoom gestureUipinchgesturerecognizer *pinchgesturerecognizer =[[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (Pinchview:)];        [View Addgesturerecognizer:pinchgesturerecognizer]; //Move gesturesUipangesturerecognizer *pangesturerecognizer =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Panview:)]; [View Addgesturerecognizer:pangesturerecognizer];}//Handling Rotation gestures- (void) Rotateview: (Uirotationgesturerecognizer *) rotationgesturerecognizer{UIView*view =Rotationgesturerecognizer.view; if(rotationgesturerecognizer.state = = Uigesturerecognizerstatebegan | | rotationgesturerecognizer.state = =uigesturerecognizerstatechanged) {View.transform=cgaffinetransformrotate (View.transform, rotationgesturerecognizer.rotation); [Rotationgesturerecognizer setrotation:0]; }}//Handling Zoom gestures- (void) Pinchview: (Uipinchgesturerecognizer *) pinchgesturerecognizer{UIView*view =Pinchgesturerecognizer.view; if(pinchgesturerecognizer.state = = Uigesturerecognizerstatebegan | | pinchgesturerecognizer.state = =uigesturerecognizerstatechanged) {View.transform=Cgaffinetransformscale (View.transform, Pinchgesturerecognizer.scale, Pinchgesturerecognizer.scale); Pinchgesturerecognizer.scale=1; }}//Handling Drag gestures- (void) Panview: (Uipangesturerecognizer *) pangesturerecognizer{UIView*view =Pangesturerecognizer.view; 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]; }}

[Self addgesturerecognizertoview:view];

If you're dealing with a picture, don't forget.
[ImageView Setuserinteractionenabled:yes];
[ImageView Setmultipletouchenabled:yes];

Define variables inside the. h file

@interface Yourviewcontroller:uiviewcontroller<uigesturerecognizerdelegate>  {      cgfloat lastScale;      CGRect oldframe;     // Save the original size      of the picture CGRect Largeframe;  // determine the maximum magnification of the  picture }  

Viewdidload inside Plus

- (void) viewdidload{[Super Viewdidload]; Showimgview= [[Uiimageview alloc] Initwithframe:<spanclass="S1">CGRectMake</span> (<spanclass="S2">0</span>, <spanclass="S2">0</span>, the,480)];    [Showimgview Setmultipletouchenabled:yes];    [Showimgview Setuserinteractionenabled:yes]; [Showimgview setimage:[uiimage imagenamed:@"1.jpg"]]; Oldframe=Showimgview.frame; Largeframe= CGRectMake (0-Screensize.width,0-Screensize.height,3* OldFrame.size.width,3*oldFrame.size.height);    [Self addgesturerecognizertoview:showimgview]; [Self.view Addsubview:showimgview];

I have modified the scaling code, added the restrictions, and other similar

//Handling Zoom gestures- (void) Pinchview: (Uipinchgesturerecognizer *) Pinchgesturerecognizer {UIView*view =Pinchgesturerecognizer.view; if(pinchgesturerecognizer.state = = Uigesturerecognizerstatebegan | | pinchgesturerecognizer.state = =uigesturerecognizerstatechanged) {View.transform=Cgaffinetransformscale (View.transform, Pinchgesturerecognizer.scale, Pinchgesturerecognizer.scale); if(ShowImgView.frame.size.width <oldFrame.size.width) {showimgview.frame=Oldframe; //make the picture smaller than the original        }          if(ShowImgView.frame.size.width >3*oldFrame.size.width) {showimgview.frame=Largeframe; } Pinchgesturerecognizer.scale=1; }  }  

"Go" uses 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.