每当我尝试使用自动布局为我已编程到屏幕的对象设置动画时,会出现很多问题。当我尝试为UIImageView设置动画时,它不会转换到下一个图像,而是复制该对象。以下是布局的代码:
icon.topAnchor.constraint(equalTo: circle.bottomAnchor, constant: 10).isActive = true
icon.centerXAnchor.constraint(equalTo: circleView.centerXAnchor).isActive = true
icon.widthAnchor.constraint(equalToConstant: 40).isActive = true
icon.heightAnchor.constraint(equalToConstant: 40).isActive = true
以下是动画的代码:
private func animateIcon(){
UIView.transition(with: icon,
duration: 0.75,
options: .transitionCrossDissolve,
animations: { self.icon.image = UIImage(named: "CultureIcon") },
completion: nil)
}
当我使用其框架将UIImageView添加到屏幕时,动画完全按预期工作。
编辑:我想我发现了这个问题。我在viewDidLayoutSubviews()中调用该视图的setup函数。一旦我从那里删除了功能,转换工作。但是,我也在使用@AmberK提供的功能,这可能有助于解决方案!
答案 0 :(得分:1)
试一试......
private func animateIcon(){
self.icon.image = UIImage(named: "CultureIcon")
UIView.transition(with: icon,
duration: 0.75,
options: .transitionCrossDissolve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
编辑:更正了格式