我在为 CollectionViewCell
内的 UIImageView
设置动画时遇到问题。我用 Auto Layout
设置了 View ,不确定这是否会导致问题。
如果尝试调用 didEndDisplaying
但没有结果。
调用单元动画的正确生命周期函数是什么?
代码:
import UIKit
class ProfileCell: UICollectionViewCell {
....
let backgroundImageView: UIImageView = {
let iv = UIImageView(frame: .zero)
iv.contentMode = .scaleAspectFill
iv.image = UIImage(named: "lustrum2017")
iv.clipsToBounds = true
return iv
}()
var blurView: UIVisualEffectView = {
let be = UIBlurEffect(style: .light)
let vv = UIVisualEffectView(effect: be)
return vv
}()
let profileImageView: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(named: "dummy")
iv.contentMode = .scaleAspectFill
iv.layer.cornerRadius = 40
iv.layer.masksToBounds = true
iv.layer.borderColor = Colors.primaryColor.cgColor
iv.layer.borderWidth = 1.0
iv.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
animateViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder has not been implemented")
}
private func animateViews() {
self.profileImageView.transform = CGAffineTransform(scaleX: 1, y: 1)
UIView.animate(withDuration: 0.45, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
private func setupViews() {
contentView.addSubview(backgroundImageView)
contentView.addSubview(blurView)
contentView.addConstraintsWithFormat("H:|[v0]|", views: backgroundImageView)
contentView.addConstraintsWithFormat("V:|[v0(150)]", views: backgroundImageView)
contentView.addConstraintsWithFormat("H:|[v0]|", views: blurView)
contentView.addConstraintsWithFormat("V:|[v0(150)]", views: blurView)
blurView.addSubview(profileImageView)
blurView.addConstraintsWithFormat("H:|-\(frame.width / 2 - 40)-[v0(80)]-\(frame.width / 2 - 40)-|", views: profileImageView)
blurView.addConstraintsWithFormat("V:|[v0(80)]", views: profileImageView)
}
....
}
您可以尝试在 UIView.animate
block 内进行转换,它应该可以工作。如下:
private func animateViews() {
self.profileImageView.transform = CGAffineTransform.identity
UIView.animate(withDuration: 0.45, animations: {
self.profileImageView.transform = CGAffineTransform(scaleX: 1, y: 1)
self.layoutIfNeeded()
}, completion: nil)
}
此外,如果您从 Storyboard/xib 创建单元格而不是通过代码手动注册单元格,awakeFromNib
可能会为您提供所需的结果。
编辑:上面的答案是假设您希望动画在第一次创建单元格后发生,因为它看起来更类似于您拥有的代码。如果您希望每次单元格出现在屏幕上时都调用它,那么您需要使用 UICollectionViewDelegate
的 collectionView(_:willDisplay:forItemAt
关于ios - 单元格中的 CGAffineTransform 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287359/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |