如何转换旋转视图的 layer.shadowOffset(x, y) 以便在图形上下文中正确绘制它?

问题描述

问题:我有一个可以旋转的 UIImageView,阴影随着旋转而移动,这很好...

UIImageView.animate(withDuration: 0.1,animations: { [self] in
            artArray[selectedArt].transform = artArray[selectedArt].transform.rotated(by: -.pi / 2)
        }

但是后来当我用 UIGraphicsBeginImageContextWithOptions draw(in:) 方法将多个图像组合成一个图像时,阴影位置错误。我知道当视图旋转时,边界也会旋转,例如:逆时针旋转 90 度的矩形 imageView 的 0,0 现在将位于左下角。

有没有办法可以转换旋转视图 layer.shadowOffset(x,y) 以反映旋转?

目前我可以手动转换它,例如在逆时针旋转 90 度的视图上的 layer.shadowOffset(x: 1,y: 1) 需要绘制为 context.setShadow(offset: (x: -1,y: 1) 以反映设备屏幕上的相同阴影位置.

有没有办法动态地做到这一点?

我无法使用 drawViewHierarchyInRect,因为在 UIGraphicsBeginImageContextWithOptions 期间我将屏幕上的图像换成全尺寸图像以导出高清照片。

任何帮助将不胜感激。

解决方案:

@Larme 为需要类似解决方案的任何人建议的使用计算的解决方案。

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)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...