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.