【发布时间】:2017-07-06 08:32:19
【问题描述】:
我在为 UIImageView 设置动画时遇到了问题 CollectionViewCell。我已经用Auto Layout 设置了视图,不确定这是否会导致问题。
如果尝试调用 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)
}
....
}
【问题讨论】:
-
如果您需要制作动画?首先你需要设置一个初始值,然后你可以动画到你想要的值。例如:将变换值设置为 0.75,然后设置为 1.0;类似的东西。
-
如你所见,我在构建 imageview 时已经这样做了。
-
这是 UICollectionViewCell 的自定义类。对吗?
标签: ios swift uicollectionviewcell uiviewanimation cgaffinetransform