IOS zooms in or out images by gesture

Source: Internet
Author: User

1. Add a variable to the. h file to record the current scale value so that the viewcontroller follows

Uigesturerecognizerdelegate proxy.

Add the following variable to. h.

Cgfloat lastscale;

2. Add the following statement to 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. How to execute this gesture

-(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. Proxy Method for adding gestures

# Pragma mark-uigesturerecognizerdelegate

-(Bool) gesturerecognizer :( uigesturerecognizer *) gesturerecognizer

Shouldrecognizesimultaneouslywithgesturerecognizer :( uigesturerecognizer *) othergesturerecognizer {


Return! [Gesturerecognizer iskindofclass: [uipangesturerecognizerclass];

}

5. Run the program and the effect (note that only images in the image center can be zoomed in, But images cannot be moved. To move images, You need to modify part of its transform code)

The figure below is from somewhere else,
Uploading on your own is troublesome.

Note: Please run on the real machine. The pinch gesture cannot be simulated in the simulator. You can also press and hold the option and shift keys to simulate the gesture of two fingers 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.