swift - 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 时有没有办法保持质量?
谢谢大家
托马斯
解决方案
解决
真的很简单。
设置 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()
}
}
}
推荐阅读
- java - 如果我将对象分配给不同类的另一个对象会发生什么?
- github - 运行 npm start 命令行后不起作用
- python - Windows 7 上的 Python-LDAP 身份验证
- c# - 条件同步,锁
- jquery - 如何使用 jquery 向我的 href 链接添加 id?
- sql-server - TSQL中嵌套sp调用中创建的临时表的范围和寿命
- python - django 不会从 statifiles_dirs 加载静态文件
- android - 我安装了 intel HAXM 并在 BIOS 中启用了虚拟技术,但仍然无法运行模拟器
- python - 一元运算符“如果 x 为 2,则 ~x == -3”
- javascript - Flask,Ajax,获取 ajax url 的 url 值