Fat*_*tie 7 xcode animation core-animation swift
想象一个高大的薄视图L(比方说,它看起来像一个梯子).
说它高100,我们将它设置为屏幕的全高.所以,在伪代码..
func expandLadder() {
view.layoutIfNeeded()
UIView.animate(withDuration: 4 ...
ladderTopToTopOfScreenConstraint = 0
ladderBottomToBottomOfScreenConstraint = 0
view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
没问题.现在说我们沿着这些线在layoutSubviews中绘制梯子.
@IBDesignable class LadderView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
func setup() {
heightNow = frame size height
print("I've recalculated the height!")
shapeLayer ...
topPoint = (0, 0)
bottomPoint = (0, heightNow)
p.addLines(between: [topPoint, bottomPoint])
shapeLayer!.path = p
shapeLayer add a CABasicAnimation or whatever
layer.addSublayer(shapeLayer!)
}
Run Code Online (Sandbox Code Playgroud)
没问题.
所以我们只是制作一个填充视图高度"heightNow"的线p.
问题当然是,当你这样做时......
func expandLadder() {
view.layoutIfNeeded()
UIView.animate(withDuration: 4 ...
ladderToTopOfScreenConstraint = 0
ladderBottomOfScreenConstraint = 0
view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
...当你这样做时,LayoutSubviews(或setBounds,或绘制#rect,或你能想到的任何其他东西)只被称为"UIView.animate动画之前和之后".这是一个真正的痛苦.
(在示例中,"实际"梯形图(shapeLayer)将跳转到下一个值,然后再为视图L的大小设置动画,即"我重新计算高度!"之前只调用一次在UIView.animate动画之后.假设你没有剪辑子视图,你会在L长度的每个动画之前看到"错误的,即将到来的"大小.)
解决方案是什么?
BTW,当然(可以这么说)你可以使用draw#rect和自己制作动画,即使用CADisplayLink(如Josh所述).所以没关系.当然,使用图层/路径绘制形状等非常容易:我的问题是如何在约束的UIView动画的每个步骤中运行代码(即上面的setup()中的代码)有问题的观点?
您描述的行为与 iOS 动画的工作方式有关。具体来说,当您在 UIAnimation 块中设置视图的框架时(这就是layoutIfNeeded 所做的——它根据自动布局规则强制同步重新计算框架),视图的框架立即更改为新值,该值是动画结束 (在layoutIfNeeded后面添加一条打印语句,您可以看到这是真的)。顺便说一句,在调用layoutIfNeeded之前,更改约束不会执行任何操作,因此您甚至不需要将约束更改放入动画块中。
然而,在动画期间,系统显示表示层而不是支持层,并且在动画持续时间内将表示层的帧值从初始值插值到最终值。这使得视图看起来像是在移动,即使检查该值将显示视图已经占据其最终位置。
因此,如果您想要动画运行时屏幕层的实际坐标,则必须使用 view.layer.presentationLayer 来获取它们。请参阅:https ://developer.apple.com/reference/quartzcore/calayer/1410744-presentationlayer
这并不能完全解决你的问题,因为我假设你想做的是随着时间的推移产生更多的层。如果您无法使用简单的插值,那么您最好在开始时生成所有图层,然后使用关键帧动画在动画进行时以一定的间隔打开它们。您可以将图层位置基于 view.layer.presentationLayer.bounds (您的子图层位于其超级图层坐标中,因此不要使用框架)。如果不知道您要实现什么目标,就很难提供准确的解决方案。
另一种可能的解决方案是将drawRect与CADisplayLink一起使用,它可以让您根据需要重新绘制每个帧,因此它更适合那些无法轻松地从帧到帧插值的东西(即游戏或在您的情况下添加/删除和动画几何体作为时间的函数)。
编辑:这是一个 Playground 示例,展示了如何与视图并行地对图层进行动画处理。
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
class V: UIViewController {
var a = UIView()
var b = CAShapeLayer()
var constraints: [NSLayoutConstraint] = []
override func viewDidLoad() {
view.addSubview(a)
a.translatesAutoresizingMaskIntoConstraints = false
constraints = [
a.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
a.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100),
a.widthAnchor.constraint(equalToConstant: 200),
a.heightAnchor.constraint(equalToConstant: 200)
]
constraints.forEach {$0.isActive = true}
a.backgroundColor = .blue
}
override func viewDidAppear(_ animated: Bool) {
b.path = UIBezierPath(ovalIn: a.bounds).cgPath
b.fillColor = UIColor.red.cgColor
a.layer.addSublayer(b)
constraints[3].constant = 400
constraints[2].constant = 400
let oldBounds = a.bounds
UIView.animate(withDuration: 3, delay: 0, options: .curveLinear, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
let scale = a.bounds.size.width / oldBounds.size.width
let animation = CABasicAnimation(keyPath: "transform.scale")
animation.fromValue = 1
animation.toValue = scale
animation.duration = 3
b.add(animation, forKey: "scale")
b.transform = CATransform3DMakeScale(scale, scale, 1)
}
}
let v = V()
PlaygroundPage.current.liveView = v
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1075 次 |
| 最近记录: |