我有一个VideoView
(这是UIView
的儿童视图)。
默认情况下,它被添加到UIView
,这是屏幕一角的小视图(我称之为ParrentView1
)。
我有一个缩小VideoView
的按钮。此按钮执行一个动作,从VideoView
中删除ParentView1
并将其添加到更大的视图(称为ParrentView2
)。
当我执行下面的代码时,它可以工作,但动画很奇怪。我需要的是从ParrentView1
到ParrentView2
的缩小动画。这是我的代码:
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
谢谢你的帮助
可能的原因是,当将其添加到另一个视图时,会为其分配不同的帧。解决方案是确保动画从原始位置开始。就像是:
CGRect originalRect = ParrentView2.convert(VideoView.frame, from:VideoView.superView);
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
VideoView.frame = originalRect;
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
一个改进点:请注意,在Swift中习惯使用小写字母来启动变量名称。当他们不这样做时会让人感到困惑。