Get the App
SLTechnology News&Howtos  ›  Development  › 

How does ios rotate a picture by rotating gestures?

Shulou Source: shulou.com Published: 2022-06-02 12:17:36 10月04日 Update

Editor to share with you how ios rotates pictures through rotating gestures. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it.

Example: rotate a picture by rotating a gesture

This example will improve the previous example by adding a rotation gesture processor based on the previous example, so that the application can not only scale the picture according to the user kneading gesture, but also rotate the picture according to the user rotation gesture.

Copy the above application and rename it RotateImage. The rest of the application basically does not need to be modified, just slightly modify the implementation part of the controller class, add a rotation gesture processor to the UIImageView control, and let the program rotate the picture according to the Radian of the gesture rotation. The following is the implementation code for the modified controller class.

Program list: codes/01/1.3/RotateImage/RotateImage/FKViewController.m

@ implementation FKViewController

UIImage* srcImage

CGFloat currentScale

CGFloat currentRotation

-(void) viewDidLoad

{

[superviewDidLoad]

[UIApplication sharedApplication] .statusBarHidden = YES

SrcImage= [UIImage paired picture picpathNAMD @ "seashore.png"]

/ / set the picture to be displayed directly in the middle (without any zooming)

Self.view.contentMode = UIViewContentModeCenter

/ / set the picture initially displayed by p_w_picpathView

Self.p_w_picpathView.p_w_picpath = srcImage

/ / set the initial scale

CurrentScale = 1

CurrentRotation = 0

/ / set p_w_picpathView to allow user interaction and support multi-touch

Self.p_w_picpathView.userInteractionEnabled = YES

Self.p_w_picpathView.multipleTouchEnabled = YES

/ / create a UIPinchGestureRecognizer gesture processor that fires scaleImage: method

UIPinchGestureRecognizer* gesture = [[UIPinchGestureRecognizer alloc]

InitWithTarget:self action:@selector (scaleImage:)]

/ / add a gesture processor for p_w_picpathView

[self.p_w_picpathView addGestureRecognizer:gesture]

/ / create a UIRotationGestureRecognizer gesture processor that fires rotateImage: method

UIRotationGestureRecognizer* rotateGesture=

[[UIRotationGestureRecognizer alloc]

InitWithTarget:selfaction:@selector (rotateImage:)]

/ / add a gesture processor for p_w_picpathView

[self.p_w_picpathViewaddGestureRecognizer:rotateGesture]

}

-(void) scaleImage: (UIPinchGestureRecognizer*) gesture

{

CGFloatscale = gesture.scale

/ / calculate the scaled target image size according to the zoom ratio of the gesture processor

CGSizetargetSize = CGSizeMake (srcImage.size.width * scale * currentScale

SrcImage.size.height * scale * currentScale)

/ / A pair of pictures are scaled and rotated

Self.p_w_picpathView.p_w_picpath = [[srcImage p_w_picpathByScalingToSize:targetSize]

P_w_picpathRotatedByRadians:currentRotation]

/ / if the gesture ends

If (gesture.state = = UIGestureRecognizerStateEnded)

{

/ / calculate the zoom ratio of the picture at the end

CurrentScale = scale * currentScale

}

}

-(void) rotateImage: (UIRotationGestureRecognizer*) gesture

{

/ / get the Radian of gesture rotation

CGFloatrotation = gesture.rotation

/ / calculate the size of the scaled target picture according to the current zoom ratio

CGSizetargetSize = CGSizeMake (srcImage.size.width * currentScale

SrcImage.size.height * currentScale)

/ / A pair of pictures are scaled and rotated

Self.p_w_picpathView.p_w_picpath = [[srcImage p_w_picpathByScalingToSize:targetSize]

P_w_picpathRotatedByRadians:currentRotation + rotation]

/ / if the rotation gesture ends

If (gesture.state = = UIGestureRecognizerStateEnded)

{

CurrentRotation = currentRotation + rotation

}

}

@ end

The bold code in the above program adds a UIRotationGestureRecognizer gesture processor to the UIImageView control. When the gesture processor detects the rotation gesture, it will activate the controller's rotateImage: method, which will rotate the picture according to the Radian of the gesture rotation (the program also uses the UIImage+FKCategory method to rotate the picture).

The above is all the content of the article "how ios rotates pictures by rotating gestures". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Gestures pictures processors processing methods examples proportions programs applications radians controllers users articles controls code content size controls goals parts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Linux Apple Microsoft