iOS zooms in or out of pictures with gestures

Source: Internet
Author: User
1. Add a variable in the. h file to record the current scale value and let this viewcontroller follow

Uigesturerecognizerdelegate Agent.

and add the following variable to. h

CGFloat Lastscale;


2. Add the following statement to the Viewdidload

UIView *holderview = [[Uiviewalloc]initwithframe:cgrectmake (0,0,320, 416)];

Uiimageview *showimgview = [[Uiimageviewalloc]initwithframe:cgrectmake (0,0,320, 416)];

Showimgview.image =img;

Showimgview.contentmode =uiviewcontentmodescaleaspectfit;

[Holderview Addsubview:showimgview];

[Showimgview release];

Uipinchgesturerecognizer *pinchrecognizer = [[Uipinchgesturerecognizeralloc]initwithtarget:selfaction: @selector ( Scagesture:)];

[Pinchrecognizersetdelegate:self];

[Holderview Addgesturerecognizer:pinchrecognizer];

[Pinchrecognizerrelease];

[Self.viewaddSubview:holderView];

[Holderview release];

3. When adding this gesture to the execution method

-(void) Scagesture: (ID) Sender {

[Self.viewbringsubviewtofront:[(uipinchgesturerecognizer*) Senderview]];

Set Lastscale to 1.0 when the finger leaves the screen

if ([(uipinchgesturerecognizer*) senderstate] = = uigesturerecognizerstateended) {

Lastscale = 1.0;

Return

}

CGFloat Scale = 1.0-(Lastscale-[(uipinchgesturerecognizer*) Senderscale]);

Cgaffinetransform currenttransform = [(uipinchgesturerecognizer*) Senderview].transform;

Cgaffinetransform newtransform = Cgaffinetransformscale (currenttransform, scale, scale);

[[(uipinchgesturerecognizer*) senderview]settransform:newtransform];

Lastscale = [(uipinchgesturerecognizer*) Senderscale];

}


4. The proxy method of adding gesture

#pragma mark-uigesturerecognizerdelegate

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer

Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer {

Return! [Gesturerecognizer Iskindofclass:[uipangesturerecognizerclass]];

}



5, run the program, the effect is as follows: (note that this can only be magnified in the image center, can not move the picture, to move the picture, you need to modify its transform part of the code)

The following figure is found from elsewhere, its own screenshot and upload more trouble, hehe

Note: Please run on the real machine, unable to simulate pinch gestures in the emulator, hold down the option and shift keys to simulate two finger gestures on the simulator.

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.