I am trying to rotate with animation a UIImageView by 5° each time it is tapped. However, each time I tap it, it seems to revert back to the original angle and start the animation from there, instead of starting from where it was already. I get that transformations need to be chained so I'm doing that, however it still doesn't work:
UIView.animate(withDuration: 1.0, animations: {
self.imgView.transform = self.imgView.transform.concatenating(CGAffineTransform(rotationAngle: CGFloat.pi/180*5))
}, completion: nil)
I found a promising option in a variant of the animate function, but that makes it look even worse (somehow even garbles up the image):
UIView.animate(withDuration: 1.0, delay: 0.0, options: .beginFromCurrentState, animations: {
self.imgView.transform = self.imgView.transform.concatenating(CGAffineTransform(rotationAngle: CGFloat.pi/180*5))
}, completion: nil)
What am I doing wrong?
BTW the view originally has a scale and translate transformation on it which is set once in viewDidLoad:
imgView.frame = CGRect(x: 0, y: view.frame.maxY-view.frame.midX, width: view.frame.maxX, height: view.frame.maxX)
imgView.transform = CGAffineTransform.identity.scaledBy(x: 14, y: 14).translatedBy(x: 0, y: 150)
I'm mentioning this just in case the problem has anything to do with this piece.
increase your angle each time you call the transform
angel = angel + 0.5
self.imgView.transform = self.imgView.transform.concatenating(CGAffineTransform(rotationAngle: CGFloat.pi/180* angel))
I found the problem. It is the initial translatedBy. If I take that out, it works perfectly. So now I know, positioning of the view should always be done via frame and/or constraints, not by translatedBy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.