ios - 为什么 UIView.animate 在 view.layer 和 sublayer 上执行不同的结果
问题描述
这是我的代码:
let colorLayer = CALayer()
colorLayer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
colorLayer.backgroundColor = UIColor.blue.cgColor
let blockView = UIView.init(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
blockView.backgroundColor = UIColor.red
self.view.addSubview(blockView)
blockView.layer.addSublayer(colorLayer)
Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { (t) in
UIView.animate(withDuration: 1.0) { [self] in
// this animation lasts 1s
// let transform = blockView.layer.affineTransform()
// blockView.layer.setAffineTransform(transform.rotated(by: CGFloat(Double.pi / 2.0)))
// while this animation 0.25s
let transform2 = colorLayer.affineTransform()
colorLayer.setAffineTransform(transform2.rotated(by: CGFloat(Double.pi / 2.0)))
} completion: { (complete) in
}
})
为什么我的自定义子图层colorLayer
旋转 0.25 秒(默认值),而不是由withDuration:
参数设置,但blockView.layer
工作正常?
解决方案
UIView 动画旨在对 UIView 对象的可动画属性的更改进行动画处理。它们不是用来为 CALayer 属性设置动画的。
UIView 动画调用可能根本没有为您更改 colorLayer 的代码设置动画。只是 CALayer 的 transform 属性支持隐式动画,因此更改它会触发隐式动画。我希望该行在 UIView 动画之外具有完全相同的效果。
我怀疑您对块视图应用仿射变换的调用是有效的,因为它结束了更改您将通过使用类似代码更改的相同变换属性
blockView.transform = blockView.transform.rotated(by: CGFloat(Double.pi / 2.0)
简短的回答:不要尝试使用 UIView 动画为图层属性设置动画。它通常不起作用。
推荐阅读
- c - 链接器是否将库函数添加到源代码中?
- python - 我的 model.fit 和 model.evaluate 无法正常工作,并且出现错误
- android - java.lang.RuntimeException:无法找到 AndroidJUnit4 的委托运行程序 androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
- javascript - React Material UI 模式未打开
- python - 在 Python 中拆分列后分配多个列名(400 列)的快速方法
- javascript - 从 C# 单元测试调用 Javascript 函数
- python - 使用单词列表从数据框中定位行
- unit-testing - 如何模拟被测同一类中的方法?
- utf-8 - 为什么即使设置了语言环境也会出现错误?
- apache - htaccess 仅限制对 index.php 的访问