First, the demand
There are two ways to achieve the ImageView zoom rotation effect:
1, the bottom layer plus scrollview, the use of ScrollView properties to achieve.
2, using gestures, kneading gestures, rotating gestures and so on.
Here I choose the second type: gesture implementation.
Ii. Description of the problem
General gesture processing, the ImageView to transform processing, but I found that each time the gesture re-processing, will overwrite the previous transform, so as not to achieve the effect of continuous gesture processing.
Like what:
I zoom in first, and then use the gesture to zoom in, will find that the picture will go back to the original position, and then zoom in, there is no way to base the first amplification method, this is not what I want.
Third, the solution
Find a lot of information, found that can be solved by Catransform3dgetaffinetransform method, this system means to obtain the previous transform position.
Then, I can each gesture after the end, first record the transform at this time, the next time to deal with, on this transform basis to continue processing, you can.
The core code is as follows:
1. Define a global variable that is used to record each tarnsform
var lasttranform3d:catransform3d?
2, Zoom, Rotation: First get the last transform, and then continue to deal with this transform
Imageview.transform = Catransform3dgetaffinetransform (lasttranform3d!). Scaledby (X:sender.scale, Y:sender.scale)
Imageview.transform = Catransform3dgetaffinetransform (lasttranform3d!). Rotated (by:sender.rotation)
All code: To achieve the zoom rotation of the picture.
///Add gesturesfunc setimagegesture () {//Zoom gestureLet pinch =Uipinchgesturerecognizer (target:self, Action: #selector (Self.handlepinchgesture (sender:))) Imageview.addgestu Rerecognizer (pinch)//Double-click Gesture, restore SizeLet Doubletap:uitapgesturerecognizer =UITapGestureRecognizer (target:self, Action: #selector (Self.handledoubletapgesture (sender:))) Doubletap.number Oftapsrequired=2Imageview.addgesturerecognizer (DOUBLETAP)//Rotate gesturesLet rotation =Uirotationgesturerecognizer (target:self, Action: #selector (Self.handlerotationgesture (sender:))) Imageview.ad Dgesturerecognizer (rotation)}
///Handling Zoom gestures/// ///-Parameter Sender:< #sender description#>func handlepinchgesture (sender:uipinchgesturerecognizer) {ifSender.state = = Uigesturerecognizerstate.began | | Sender.state = =uigesturerecognizerstate.changed {ifLasttranform3d = =Nil {imageview.transform=Cgaffinetransform (ScaleX:sender.scale, Y:sender.scale)}Else{imageview.transform= Catransform3dgetaffinetransform (lasttranform3d!). Scaledby (X:sender.scale, Y:sender.scale)}}Else ifSender.state = =uigesturerecognizerstate.ended{Lasttranform3d=ImageView.layer.transform}}
/// Restore Pictures /// /// < #sender description#> func handledoubletapgesture (sender:uitapgesturerecognizer ) { = cgaffinetransform.identity = Nil }
///Handling Rotation gestures/// ///-Parameter Sender:< #sender description#>func handlerotationgesture (sender:uirotationgesturerecognizer) {ifSender.state = = Uigesturerecognizerstate.began | | Sender.state = =uigesturerecognizerstate.changed {ifLasttranform3d = =Nil {imageview.transform=cgaffinetransform (rotationAngle:sender.rotation)}Else{imageview.transform= Catransform3dgetaffinetransform (lasttranform3d!). Rotated (By:sender.rotation)}}Else ifSender.state = =uigesturerecognizerstate.ended{Lasttranform3d=ImageView.layer.transform}}
IOS Transform fixed multiple rotation zoom for picture rotation zoom effect