Problem: I have a UIImageView that can be rotated, the shadow moves with the rotation which is fine...
UIImageView.animate(withDuration: 0.1, animations: { [self] in
artArray[selectedArt].transform = artArray[selectedArt].transform.rotated(by: -.pi / 2)
}
But later when I combine multiple images into one image in UIGraphicsBeginImageContextWithOptions draw(in:) method, the shadow is in the wrong position. I understand that when a view is rotated the bounds is also rotated for example: 0,0 of a rectangular imageView that has been rotated 90 degrees anti-clockwise is now going to be in the bottom left corner.
Is there a way I can convert a rotated views layer.shadowOffset(x, y) to reflect the rotation?
Currently I can convert it manually for example layer.shadowOffset(x: 1, y: 1) on a view that has been rotated 90 degrees anti-clockwise needs to be drawn as context.setShadow(offset: (x: -1, y: 1) to reflect the same shadow position that is on the device screen.
Is there a way I can do this dynamically?
I can't use drawViewHierarchyInRect as during UIGraphicsBeginImageContextWithOptions I am swapping out the images on screen for full size images in order to export HD photos.
Any help would be greatly appreciated.
Solution:
Solution using calculations @Larme suggested for anyone that needs similar solution.
func correctOffset(angle: CGFloat, offset: CGSize) -> CGSize {
let unroundedX = offset.height * CGFloat(sinf(Float(angle))).rounded() + offset.width * CGFloat(cosf(Float(angle))).rounded()
let unroundedY = offset.height * CGFloat(cosf(Float(angle))) - offset.width * CGFloat(sinf(Float(angle)))
x = round(unroundedX * 10) / 10
y = round(unroundedY * 10) / 10
return CGSize(width: x, height: y)
}