Home > Net >  Rotate Image 5 degrees each time user clicks button in swift
Rotate Image 5 degrees each time user clicks button in swift

Time:04-01

I am creating a custom image editor for a project where I am stuck at rotating images by 5 degrees forward and backward. here is what I have tried so far

 1. self.cropImageView.transform =
    self.cropImageView.transform.rotated(by: CGFloat(Double(5) *
    .pi/180)) 
 2. self.cropImageView.transform =
    CGAffineTransform(rotationAngle: CGFloat(Double(5) * .pi/180) ) 

Here is the issue in 1 case image only rotates one time and in another case, the image rotates but the image size is getting smaller as I rotate it.

enter image description here

CodePudding user response:

The code will only rotate the imageView,if the image size changes,you may have missed some code.

func rotate(isForward: Bool) {
    self.cropImageView.transform = self.cropImageView.transform.rotated(by: (isForward ? -1 : 1)  * .pi * (5.0 / 180.0))
}

CodePudding user response:

In case 1 you telling to rotate 5 degrees from original so nothing gonna happen after first try, to solve this you can set variable to remember the last time's number (for example 5) and by pressing the buttons increase or decrease it by 5 (like 10 or 0)

self.cropImageView.transform =
    self.cropImageView.transform.rotated(by: CGFloat(Double(var_you_keep_prev_degree) *
    .pi/180))
  • Related