首页 > 解决方案 > CGAffineTransform - 使用后像素化

问题描述

问题:

使用 CGAffineTransform 时,我失去了 UIView 及其所有子视图的质量。

代码

我目前正在运行以下代码来缩放和移动我的 UIView。

    func animate(show: Bool, navigationController: UINavigationController) {
    let viewWidth = UIScreen.main.bounds.width
    let scale: CGFloat = 0.7

    UIView.animate(withDuration: 0.5, animations: {

        if !show {
            var t = CGAffineTransform.identity
            t = t.scaledBy(x: scale,
                           y: scale)
            t = t.translatedBy(x: viewWidth * 0.6, y: 0)
            navigationController.view.transform = t
        } else {
            navigationController.view.transform = .identity
        }

    }) { (complete) in
        if show {
            let sideMenuView = UIApplication.shared.keyWindow?.viewWithTag(-253)
            sideMenuView?.removeFromSuperview()
        }
    }
}

但是当我回到我原来的状态时,一切都是像素化和低质量的。

我在手机和模拟器上都试过了。我已经删除了比例因子,问题仍然存在。我还尝试将值设置为原始值。

例子

     var t = CGAffineTransform.identity
        t = t.scaledBy(x: 1,
                       y: 1)
        t = t.translatedBy(x: 0, y: 0)
        navigationController.view.transform = t

问题

使用 CGAffineTransform 时有没有办法保持质量?

谢谢大家

托马斯

标签: swiftcore-graphicscgaffinetransform

解决方案


解决

真的很简单。

设置 rasterizationScale 解决了这个问题。

    func animate(show: Bool, navigationController: UINavigationController) {
    let viewWidth = UIScreen.main.bounds.width
    let scale: CGFloat = 0.7
    navigationController.view.layer.rasterizationScale = UIScreen.main.scale

    UIView.animate(withDuration: 0.5, animations: {

        if !show {
            var trans = CGAffineTransform.identity
            trans = trans.scaledBy(x: scale,
                           y: scale)
            trans = trans.translatedBy(x: viewWidth * 0.6, y: 0)
            navigationController.view.transform = trans

        } else {
            navigationController.view.transform = .identity
        }

    }) { (complete) in
        if show {
            let sideMenuView = UIApplication.shared.keyWindow?.viewWithTag(-253)
            sideMenuView?.removeFromSuperview()
        }
    }
}

推荐阅读